Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Inputing info into Nested Arrays

by Cirollo (Friar)
on Aug 21, 2000 at 20:21 UTC ( [id://28834]=note: print w/replies, xml ) Need Help??


in reply to Inputing info into Nested Arrays

If you want to create a multidimensional array, you do it like this:
@array = ( [ "foo", "bar"], [ "abc", "def"] );
You can access elements with 2 array subscripts - $array[0][0] is "foo", $array[0][1] is "bar", $array[1][0] is "abc", etc.

Supposing that your file of projects had each project on 1 line, you could do this:

while ( <FILE> ) { push @array, [ split ]; }

Then to print it back out,

for $i (0..$#array) { for $j (0..$#{$array[$i]}) { print "Element $j of project $i is $array[$i][$j]\n"; } }

See The Perl Data Structures Cookbook for more info.

Replies are listed 'Best First'.
RE: Re: Inputing info into Nested Arrays
by She-wolf (Sexton) on Aug 21, 2000 at 20:45 UTC
    The whole:
    @array = ( [ "foo", "bar"], [ "abc", "def"] );

    thing seems to work just for small stuff, is there another way that will be a little easier for a large number of references?

    Also, I don't see how the while ... bit will work.(please excuse me, I'm a newbie)

    She-wolf
    "Wha? I don't get it."

      The while... bit splits each line up and puts the resulting array into an element of @array.

      split with no arguments takes a line from $_ and splits it on whitespace (e.g, /\s+/) after disregarding leading spaces. So, that creates the 'inner' array from each line (each word in the line is 1 element), and then the 'outer' array is created with each line as an element. So,

      This is a test. This is line #2
      would be read in like this:
      $array[0][0] = "This"; $array[0][1] = "is"; $array[0][2] = "a"; $array[0][3] = "test"; $array[1][0] = "This"; $array[1][1] = "is"; $array[1][2] = "line"; $array[1][3] = "#2";

      And of course, you are going to have to change this based on your data file's structure.

        So basically I'd have to have 11 columns and 100 rows?

        She-wolf
        "Wha? I don't get it."

Log In?
Username:
Password:

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

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

    No recent polls found