Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

how to assign map results to arrays

by perlfan99 (Sexton)
on Jul 29, 2008 at 06:48 UTC ( [id://700739]=perlquestion: print w/replies, xml ) Need Help??

perlfan99 has asked for the wisdom of the Perl Monks concerning the following question:

I want to assign map results to 2 different arrays, like below

$num1 = "1;2;3"; $num2 = "4;5;6"; (@ar1, @ar2) = map { split(';', $_) } ($num1, $num2); print "ar1 size = " . scalar(@ar1) . "\n"; print "ar2 size = " . scalar(@ar2) . "\n"; output: ar1 size = 6; ar2 size = 0;
is there a way i can assign
@ar1 = (1, 2, 3) and @ar2 = (4, 5, 6)

thanks!

Replies are listed 'Best First'.
Re: how to assign map results to arrays
by ikegami (Patriarch) on Jul 29, 2008 at 06:53 UTC

    You wouldn't have this problem if you were using $var[n] instead of $varn.

    my @arrays = map { [ split /;/, $_ ] } @nums;

    But to answer your question:

    my $num1 = "1;2;3"; my $num2 = "4;5;6"; my (@ar1, @ar2); for ( [ $num1, \@ar1 ], [ $num2, \@ar2 ], } { my ($num, $ar) = @$_; @$ar = split /;/, $num; }

    By the way, don't fool yourself into thinking the first argument of split is a text string. split expects a regexp.

Re: how to assign map results to arrays
by pc88mxer (Vicar) on Jul 29, 2008 at 06:51 UTC
    How about:
    @ar1 = split(';', $num1); @ar2 = split(';', $num2);

    Update: if you want to know how to get

    # given: @num = ("1;2;3", "4;5;6"); $array[0] = [1, 2, 3]; $array[1] = [4, 5, 6];
    then you want:
    @array = map { [ split(';', $_) ] } (@num);
    The square brackets around split create an array reference out of what split returns.
Re: how to assign map results to arrays
by GrandFather (Saint) on Jul 29, 2008 at 12:23 UTC
    (@ar1[0..2], @ar2[0..2]) = map { split(';', $_) } ($num1, $num2);

    Perl reduces RSI - it saves typing
Re: how to assign map results to arrays
by perlfan99 (Sexton) on Jul 29, 2008 at 07:34 UTC
    i like reference approach and it works, but i have to dereference it though, thanks for the tip

     ($ar1, $ar2) = map { [split(';', $_)] } ($num1, $num2);

    not quite understand why dose Perl flatten the results.

      not quite understand why dose Perl flatten the results.

      Because Perl functions can only return one thing: a list of scalars. A list of scalars is created from the two arrays and returned by map.

      Then you ask to assign that list to a pair of arrays. There's no way for Perl to know how to split that list to assign it to two arrays, so it assigns the entire list to the first array.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-24 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found