Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Joining Arrays?

by kyle (Abbot)
on Dec 20, 2008 at 21:55 UTC ( [id://731815]=note: print w/replies, xml ) Need Help??


in reply to Joining Arrays?

You could do it with nested maps:

open my $url_fh, '<', 'urls.txt' or die "Can't read 'urls.txt': $!"; chomp( my @urls = <$url_fh> ); close $url_fh or die "close() failed: $!"; open my $code_fh, '<', 'data.txt' or die "Can't read 'data.txt': $!"; chomp( my @codes = <$code_fh> ); close $code_fh or die "close() failed: $!"; my @joined = map { my $url = $_; map { $url . $_ } @codes } @urls;

Note that this actually creates an array for the results, while a foreach solution won't.

Replies are listed 'Best First'.
Re^2: Joining Arrays?
by graff (Chancellor) on Dec 21, 2008 at 03:52 UTC
    There's something about nested maps that strikes me as unsavory. Sure, there's nothing syntactically wrong with it, but then there's nothing syntactically wrong with a sentence like "The cat the dog the man hit bit died."

    In the case of nested maps, keeping track of what each $_ is holding involves an extra cognitive load. It's simply that it takes the reader a fair bit longer than normal to figure out the intent, and for that reason, I'd prefer to avoid it.

      I agree with that. It would be nice if map (and grep) could take a variable like foreach does.

      # this doesn't work my @joined = map my $url { map my $code { $url . $code } @codes } @urls;

        I guess, but I actually find that version harder to read than the first version. For one thing, it has no indentation options that aren't kind of terrible.

        For the general, non-nested versions, it might be nicer.

        for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

        If the map BLOCK is more complicated than [insert personal threshold here], or they are nested I just declare the variable myself.

        @joined = map { my $url = $_; map { my $code = $_; $url . $code; } @codes } @urls;
      ...nothing syntactically wrong with a sentence like "The cat the dog the man hit bit died."

      Ah. Fluent reverse Polish. Or is it Yoda egg-nog on (too much) ?

      I've tried to parse this... putting the brackets back in I get: died(the cat bit (the dog hit the man)) -- I'm assuming: (a) that each 'the' is part of the name of each object/subject; (b) that 'died' is a unary (intransitive); and (c) that 'bit' and 'hit' are binary operations (transitive).

      So much for syntax... semantics-wise, assuming that the 'hit' and 'bit' operators return the object modified by the operation inflicted upon it by the subject...

      ...no wonder the man died. What on earth had he done to upset both the cat and the dog ? And what did the dog hit him with ?

      Of course, being reverse polish the operators may themselves be reverse -- so the cat gets it because the man hit the dog ? What kind of twisted world is this ?

Re^2: Joining Arrays? (perl6)
by eric256 (Parson) on Dec 22, 2008 at 03:32 UTC

    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
      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.

      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://731815]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found