well the code loads a file into an array line by line. However it certainly isn't the most optimal way to do this. At least I certainly wouldn't do it this way.
sub load_array($$) { #the first element we pass to this function is a #array reference, the second is the name of a file my ($a_ref, $file) = @_; #attempt to open the file or quit executing our program open (FILE, $file) or die "Can't open $file:$!\n"; #take all the lines in the file, and put them into the #array that $a_ref points to. @{$a_ref} = <FILE>; # close the file... duh I guess close FILE; # remove all of the carriage returns from the end of # each element in the array chomp @{$a_ref}; } # to call this routine my @array; &load_array(\@array, # pass in the reference to the array "test.txt"); # the name of our file
Personally I think I would rewrite this to do something like:
sub load_array { my $file = shift; # the first parameter is our file my @array; open(IN, $file) or die "Can't open $file: $!\n"; # for every line in the file... while(<IN>) { chomp; # remove the trailing carriage return # could also be chomp $_; push @array; # put it into our array # could also be push @array, $_; } # while close(IN); return @array; } # load_array # to run this routine... my @array = &load_array("test.txt");

In reply to Re: Explaining by gaspodethewonderdog
in thread Explaining by Anonymous Monk

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.