Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: looping over multidimensional arrays

by BillKSmith (Parson)
on Sep 20, 2018 at 20:12 UTC ( #1222745=note: print w/replies, xml ) Need Help??


in reply to looping over multidimensional arrays

You can usually replace the innermost loop with a map.
?type 1222715.pl use strict; use warnings; use Data::Dumper; my @array = ( [qw(0a 0b 0c 0d 0e 0f 0g 0h 0i 0j 0k)], [qw(1a 1b 1c 1d 1e 1f 1g 1h 1i 1j 1k)], [qw(2a 2b 2c 2d 2e 2f 2g 2h 2i 2j 2k)], [qw(3a 3b 3c 3d 3e 3f 3g 3h 3i 3j 3k)], [qw(4a 4b 4c 4d 4e 4f 4g 4h 4i 4j 4k)], ); my @array_new; foreach my $vec (@array) { push @array_new, [map {myfunc($_)} @$vec]; } print Dumper(\@array_new); sub myfunc { return "$_[0]_new"; } ?perl 1222715.pl $VAR1 = [ [ '0a_new', '0b_new', '0c_new', '0d_new', '0e_new', '0f_new', '0g_new', '0h_new', '0i_new', '0j_new', '0k_new' ], [ '1a_new', '1b_new', '1c_new', '1d_new', '1e_new', '1f_new', '1g_new', '1h_new', '1i_new', '1j_new', '1k_new' ], ... ]; ?
Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2019-07-15 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?