Re: Sorting Multidimentional Arrays

2016-10-01 Thread Joan Pujol

El Saturday, 01 de October del 2016 a les 20:44, Moritz Lenz va escriure:


On 01.10.2016 19:57, mimosinnet wrote:

@opposite = @opposite.sort({@$^a[3]});


For the record, you can simplify this a bit:

@opposite = @opposite.sort(*[3]);


Waw! It works! Many thanks! 

I am still puzzled by the asterisk. I will have to dig more into the 
documentation :D :)


Cheers! 


for @opposite -> $line { say $line; }
---

I have adapted this from:
http://www.wellho.net/resources/ex.php?item=p600/mapper

I wanted to ask about the syntax, buy I have understood it while
writing the message ;) :D


I've had the same experience a few times :-)

Cheers,
Moritz

--
Moritz Lenz
https://deploybook.com/ -- https://perlgeek.de/ -- https://perl6.org/


--
Joan Pujol Tarrés
Departament de Psicologia Social
Facultat de Psicologia, Universitat Autònoma de Barcelona
Despatx: B5-036 (Passadís Departament Psicologia Social)
Edifici B, 08193 Bellaterra (Barcelona), Fax: +34 935812001
http://psicologiasocial.uab.es/ca/user/43
http://psicologiasocial.uab.es/fic
http://orcid.org/-0002-0462-3278
--
signature.asc -> http://www.thelounge.net/signature.asc.what.htm
--
()  campaña lazo ascii  |  por favor, enviar correo ascii
/\  www.asciiribbon.org |  por favor, enviar adjuntos en formato libre


Re: What are variables/parameters that start with a pipe | char

2016-10-01 Thread Moritz Lenz
Hi,

On 01.10.2016 04:22, Francis (Grizzly) Smit wrote:
> I keep finding stuff like this:
> 
> multi method spurt(IO::Path:D: Blob $contents, :$bin, |c)
> multi method spurt(IO::Path:D: Cool $contents, :$bin, |c)
> 
> 
> but I cannot find the |c syntax in the docs I have googled but no good 
> a pointer or link would be good. 

Just for the record, a good approach to find such a thing would be to
type the defining character, here the |, into search box on
https://doc.perl6.org/.

It doesn't work for everything, but for | it does give you, among other
things, "| (parameter)", which points to relevant documentation.

Cheers,
Moritz


-- 
Moritz Lenz
https://deploybook.com/ -- https://perlgeek.de/ -- https://perl6.org/


Re: Sorting Multidimentional Arrays

2016-10-01 Thread Larry Wall
On Sat, Oct 01, 2016 at 07:57:34PM +0200, mimosinnet wrote:
: @opposite = @opposite.sort({@$^a[3]});

I'd probably write that as:

@opposite .= sort: { $^a[3] }

or maybe just

@opposite .= sort( *[3] );

Larry


Sorting Multidimentional Arrays

2016-10-01 Thread mimosinnet
I have been searching on how to sort multidimensional arrays. The 
following code works and sorts the fourth column of the array:


---
#!/usr/bin/env perl6
# sort multidimensional array 


my @opposite = (
<8976 ABD AB11 6LX NJ >,
<8860 PLN AB12 4JS NO >,
<8905 DYC AB21 7EQ NJ >,
<8964 STN AB39 2NE NO >,
);

for @opposite -> $line { say $line; }
say "-" x 22;
@opposite = @opposite.sort({@$^a[3]});

for @opposite -> $line { say $line; }
---

I have adapted this from: 
http://www.wellho.net/resources/ex.php?item=p600/mapper


I wanted to ask about the syntax, buy I have understood it while 
writing the message ;) :D 


Leve the code just in case is useful for somebody else.

Cheers!

--
(≧∇≦) Mimosinnet (Linux User: #463211)



Initializing a CStruct attribute of a CStruct

2016-10-01 Thread Fernando Santagata
Hello,

I'm trying to do something complex (at least for me :-) with NativeCall.

Let's start with plain Perl6. This:

class test1 {
  has Int  $.a;
  has Int  $.b;
}

class test2 {
  has Int   $.c;
  has test1 $.d;
}

my test2 $t2 .= new(:3c, d => test1.new(:1a :2b));
dd $t2;

outputs:

test2 $t2 = test2.new(c => 3, d => test1.new(a => 1, b => 2))


But I need to do that with CStructs. This code:

use NativeCall;

class test1 is repr('CStruct') is export {
  has uint64  $.a;
  has uint64  $.b;
}

class test2 is repr('CStruct') is export {
  has uint64  $.c;
  has test1   $.d;
}

my test2 $t2 .= new(:3c, d => test1.new(:1a :2b));
dd $t2;

doesn't really work and outputs this:

Cannot modify an immutable test1


Adding a BUILD submethod isn't enough:

submethod BUILD(:$!c, :$!d) { }

What I get is:

CStruct can't perform boxed get on flattened attributes yet


and this:

submethod BUILD(:$c, :$d){
  $!c = $c;
  $!d := $d;
}

doesn't work either:

Can only store CStruct attribute in CStruct slot in CStruct


But this one does:

submethod BUILD(:$c, :$d){
  $!c = $c;
  $!d := test1.new(a => $d.a, b => $d.b);
}

Only, I don't want to initialize all the attributes one by one...
Is there a less cumbersome way to do that initialization?

Thanks!

-- 
Fernando Santagata