Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Array Scopes?

by mykl (Monk)
on Nov 26, 2009 at 12:48 UTC ( [id://809551]=note: print w/replies, xml ) Need Help??


in reply to Array Scopes?

So to combine what the monks above have said, if you wanted an array of all the coursenames from each line, an array of buildings etc., an appoach might be

while (<>) { my ($coursename, $building, $room, $day, $time, $name) = split /, /, $line; push @coursename, $coursename; push @building, $building; push @room, $room; push @day, $day; push @time, $time; push @name, $name; }

This still has some repetition (the 6 pushes) but this is inevitable because of your decision to hold the pieces of data in separate arrays. An array of hashes might be nicer, assuming that each building, room etc. belong with the coursename that is on the same line:

my @course_info; while (<>) { my ($coursename, $building, $room, $day, $time, $name) = split /, /, $line; push @course_info, { coursename => $coursename, building => $building, room => $room, day => $day, time => $time, name => $name, }; }

or if the order of the courses is not important, the course names are unique and you want to access the data by course name, a hash of hashes:

my %course_info; while (<>) { my ($coursename, $building, $room, $day, $time, $name) = split /, /, $line; $course_info{$coursename} = { building => $building, room => $room, day => $day, time => $time, name => $name, }; }

Replies are listed 'Best First'.
Re^2: Array Scopes?
by phantom20x (Acolyte) on Nov 30, 2009 at 02:56 UTC
    Thanks for all the input, going to play with it shortly, especially the hash bit(haven't done much in relation to hashes).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://809551]
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: (5)
As of 2024-04-25 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found