Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Array Scopes?

by phantom20x (Acolyte)
on Nov 26, 2009 at 04:59 UTC ( [id://809480]=perlquestion: print w/replies, xml ) Need Help??

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

I was writing code for an assignment(already turned in), and a portion of it looked like below(ugly I know). The question is in regards to upon leaving the while loop, I could no longer access the arrays in their entirety. Only one element was left. Before leaving the while loop, all the elements were there in each array and accounted for. This seemed like a noob scoping problem but I couldn't seem to find any info on it, so I am asking the wise. Thanks in advance.

while(<>){ @coursename=split(/(\w+),\s\w+,\s\w+,\s\w+,\s\w+,\s\w+/); @building=split(/\w+,\s(\w+),\s\w+,\s\w+,\s\w+,\s\w+/); @room=split(/\w+,\s\w+,\s(\w+),\s\w+,\s\w+,\s\w+/); @day=split(/\w+,\s\w+,\s\w+,\s(\w+),\s\w+,\s\w+/); @time=split(/\w+,\s\w+,\s\w+,\s\w+,\s(\w+),\s\w+/); @name=split(/\w+,\s\w+,\s\w+,\s\w+,\s\w+,\s(\w+)/); }

Replies are listed 'Best First'.
Re: Array Scopes?
by cdarke (Prior) on Nov 26, 2009 at 10:12 UTC
    Your problem is nothing to do with scope, you are overwriting each array on every iteration of the loop.
    You need to add to each array, not reassign it. For example:
    push @coursename,split(/(\w+),\s\w+,\s\w+,\s\w+,\s\w+,\s\w+/);


Re: Array Scopes?
by bichonfrise74 (Vicar) on Nov 26, 2009 at 06:17 UTC
    Why can't use transform your code into something like this?
    #!/usr/bin/perl use strict; my @record = split( /,\s/ ) while (<>);
Re: Array Scopes?
by 7stud (Deacon) on Nov 26, 2009 at 07:56 UTC

    A literal translation of your code would be:

    my $line = "math101, A, 203, MWF, 9:30am, Einstein"; my ($coursename, $building, $room, $day, $time, $name) = split /, /, $line; say $building; say $name;

    But of course, there's no reason to declare six variables when you could store everything in one array variable.

    As for any scope problem, you need to prove your case. That means you need to post a complete example that anyone can run, which displays some output inside the loop, then displays some output outside the loop.

Re: Array Scopes?
by mykl (Monk) on Nov 26, 2009 at 12:48 UTC

    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, }; }
      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: perlquestion [id://809480]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found