Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Joining Arrays? (perl6)

by eric256 (Parson)
on Dec 22, 2008 at 03:32 UTC ( [id://731957]=note: print w/replies, xml ) Need Help??


in reply to Re: Joining Arrays?
in thread Joining Arrays?

Perl6 provides a pretty handy way to do this. It has X which I beleive is pronounced "cross" so that you can do: (source)

use v6; my @urls = ('http://www.something.com/blah.aspx?code=', 'http://www.somethingelse.com/stuff.aspx?thing='); my @ids = ('375035304','564564774','346464646'); my @combined = (@urls X @ids).map: {$^a ~ $^b}; .say for @combined;

Which outputs the following on Rakudo already:

http://www.something.com/blah.aspx?code=375035304 http://www.something.com/blah.aspx?code=564564774 http://www.something.com/blah.aspx?code=346464646 http://www.somethingelse.com/stuff.aspx?thing=375035304 http://www.somethingelse.com/stuff.aspx?thing=564564774 http://www.somethingelse.com/stuff.aspx?thing=346464646

The $^a and $^b in the map block tell it to take two elements at a time, while the X in the parenthesis tells it to cross join the two arrays. Very very handy for this kind of thing.


___________
Eric Hodges

Replies are listed 'Best First'.
Re^3: Joining Arrays? (perl6)
by TimToady (Parson) on Dec 23, 2008 at 02:25 UTC
    Such cross-like operations are common enough that there is also a cross meta-operator in Perl 6 to trick an ordinary binary operator into doing it. So instead of saying:
    my @combined = (@urls X @ids).map: {$^a ~ $^b};
    you can just say
    my @combined = @urls X~ @ids;
    That is, just put X in front of the ordinary operator to make it a cross operator.

    Update: changed old X~X form to new X~ form.

Re^3: Joining Arrays?
by nagalenoj (Friar) on Dec 22, 2008 at 10:00 UTC
    Interesting. But, unfortunately I am using perl 5.8. Where can I read about the new arrivals in perl 6 version?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://731957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-18 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found