Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Joining Arrays? (perl6)

by eric256 (Parson)
on Dec 22, 2008 at 03:32 UTC ( #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? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2022-08-17 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?