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

Re: Arrays in arrays, how to access them

by viored (Novice)
on Oct 29, 2013 at 23:17 UTC ( #1060237=note: print w/ replies, xml ) Need Help??


in reply to Arrays in arrays, how to access them

Thanks for the data structures link. I'll be reading through it. But maybe I'm going about my problem in the wrong way. So, what I'm trying to do, in simplest terms, is have many arrays, with each array containing multiple strings, and then I want to be able to do operations on those strings in an iterative fashion. I thought an array of arrays would do it, but I'm not very familiar with perl. Would you suggest that I store things a different way?


Comment on Re: Arrays in arrays, how to access them
Re^2: Arrays in arrays, how to access them
by Anonymous Monk on Oct 30, 2013 at 00:29 UTC

    So you want to have a bunch of strings

    So you want to stuff those strings under one thing, one bucket, one box, one variable

    So you want to have an array-of-strings

    So you have more than one array-of-strings

    So keeping more than one array-of-strings in an array, seems natural

    Unlesss, you want to name each and every one of those arrays of bunches of things, then keep them in a hash

    #!/usr/bin/perl -- use strict; use warnings; my @feet = qw/ left right /; my @shoes = qw/ loafer sneaker boot /; my @lowers = ( \@feet, \@shoes ); ## $lower{feet} is prounounced lower-of-feet ## $lower{shoes} is prounounced lower-of-shoes my %lower = ( feet => [ @feet ], shoes => [ @shoes ] ); print "$lower{feet}[0] $lower{shoes}[0]\n"; print "$lower{feet}[1] $lower{shoes}[1]\n"; print "----\n"; for my $lower ( @lowers ){ for my $low ( @$lower ){ print " $low "; } print "\n"; } print "----\n"; ## while( my( $key, $value ) = each %lower ){ while( my( $lower, $of_array ) = each %lower ){ print "$lower $_\n" for @$of_array; }
    foreach, each, Tutorials: Data Type: Array, references quick reference

    More elaborate and coprehensive tutorial in http://learn.perl.org/books/beginning-perl/, Learn Perl in about 2 hours 30 minutes, perlintro, chromatics free book Modern Perl a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.

Re^2: Arrays in arrays, how to access them
by Laurent_R (Parson) on Oct 30, 2013 at 07:17 UTC

    Anonymous Monk has proposed a hash of arrays data structure, which enables you to name the sub_arrays. This is fine if you need to names these arrays. If you don't particularly need to name them, then the array of arrays structure that I outline above is just as fine for what you want to do. You could have something like this:

    my @array = ([$str_1_1, $str_1-2], [$str_2_1, $str_2_2]);

    It is then easy to process the strings of any subarray.

    Update 08:35 UTC: added the following examples under the Perl debugger:

    DB<1> @array = (["str_1_1", "str_1-2"], ["str_2_1", "str_2_2"]) DB<2> DB<2> x @array 0 ARRAY(0x253a70) 0 'str_1_1' 1 'str_1-2' 1 ARRAY(0x58f8a8) 0 'str_2_1' 1 'str_2_2' DB<3> x $array[1] 0 ARRAY(0x58f8a8) 0 'str_2_1' 1 'str_2_2' DB<4> p "$array[1][$_] \n" for 0..1 str_2_1 str_2_2

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-12-27 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls