Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Merging arrays

by LanX (Chancellor)
on Jun 27, 2013 at 22:53 UTC ( #1041115=note: print w/replies, xml ) Need Help??

in reply to Merging arrays


...your AoH w/o extra modules and complicated maps using a temporary hash-slice:

DB<100> @array = qw( 111 222 333 444 555 888 ); => (111, 222, 333, 444, 555, 888) DB<101> @array2 = qw( acct1 acct2 acct3 acct4 acct5 acct8 ); => ("acct1", "acct2", "acct3", "acct4", "acct5", "acct8") DB<102> @temp{@array}=@array2 => ("acct1", "acct2", "acct3", "acct4", "acct5", "acct8") DB<103> @AoH = map { { $_ => $temp{$_} } } @array => ( { 111 => "acct1" }, { 222 => "acct2" }, { 333 => "acct3" }, { 444 => "acct4" }, { 555 => "acct5" }, { 888 => "acct8" }, )

(ignoring your typo)

¹) Note elements of @array must be unique ...

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Merging arrays
by AnomalousMonk (Canon) on Jun 28, 2013 at 02:19 UTC
    ... elements of @array must be unique...

    That strikes me as a drawback. After all, there seems to be nothing inherent in the organization of the two source arrays to suggest such a limitation. Why shouldn't one be allowed to end up with data like:

    my @AoH = ( { 111 => "acct1" }, { 111 => "hoo" }, { 111 => "ha" }, { 111 => "foo" }, { ... => "..." }, { 888 => "acct8" }, );
      > Why shouldn't one be allowed to end up with data like:

      No special reason.

      Sometimes one has data structure which have to be unique, then using a hash is handy.

      Sometimes not ... I'm just listing another way to do it for one of those loosely defined PM requirements... :)

      FWIW I'll change my post to make the restriction clearer ...


      The question is rather how to design functional constructs like map which can easily (idiomatically) be used/combined to create nested data-structures which solve all possible requirements (sorted, nested, unique, non-string keys, ...).

      Thats more a design than an implementation question, I rarely see people using List::MoreUtils here.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        ... loosely defined ... requirements...

        Yes, that's always the tricky bit: to divine what the OPer really wants... or, better yet, needs!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041115]
[Corion]: A good morning!
[Discipulus]: good morning Corion!
[Corion]: I spent parts of yesterday digging into Filter::Simple to make it not change the line numbers of unfiltered code, but I think I need regex help (which will come in a SoPW I guess). I tried @lines = /((?:"...."|[^" \n]+)\n)/gs, but that matched on...
[Corion]: ... foo "\0\0\0\n" bar\n by matching the first \n instead of matching the four-bytes-in- double-quotes part
[Corion]: ... and I still don't understand why ;)
[Corion]: This is because Filter::Simple does some string-trickery, replacing all string literals with "quoted packed numbers"

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-01-23 08:02 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (191 votes). Check out past polls.