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, }; }

In reply to Re: Array Scopes? by mykl
in thread Array Scopes? by phantom20x

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.