... why I can't do slurp while populating the data var?

A lot depends on the organization of data in your input file. (These little details really do matter.) The following discussion assumes you have not changed the default value of  $/ which is  "\n" (newline); see perlvar.

If you have a file in which each individual number is on a separate line that is terminated by a newline, the statement
    # list context file read -- effective slurp: entire file is read
    my @each_element_has_a_line = <$input_filehandle>;
reads each and every line of the file as a string into a separate element of the array. This is the effect of reading the file in list context. See readline. If this operation is followed by a
    chomp @each_element_has_a_line;
statement, each element of the array/line read from the file/string has a newline removed from the end, if one was present. See chomp. If you're lucky, you now have an array of strings which each look like a number to Perl, and so can be handled by numeric operators like <=>.

If you have a file organized as above, the statement
    # scalar context slurp
    my $all_lines_in_a_single_string = do { local $/;  <$input_filehandle>; };
reads the entire content of the file, all the lines, newlines and all, into a single string. This is the effect of reading the file in scalar context with $/ undefined; this is referred to as file slurp mode. If this operation is followed by a
    chomp $all_lines_in_a_single_string;
a single newline, if present, will be removed from the end of this single string. What remains requires further processing to extract the digit groups (numbers) from the string to an array so they can be sorted.

If, OTOH, you have your data in a file organized as a single line of digit groups separated by whitespace(s) (which seems to have been the case with your OPed data), reading the file in slurp mode or not really doesn't matter because there's only one thing there to read to begin with. (This is your answer to the quoted question.) If you read to an array, you get (somewhat confusingly) an array with a single element: a string that is the single string that was in the file. If you read to a scalar... again, a single string. chomp-ing this single element/scalar/string may or may not help you; you still must do further processing to extract the numbers.

I urge you to do some experimentation with these various permutations of file organization, slurping and file-read context (list vs. scalar). I also urge you read about the general topic of context in Perl (the Context tutorial is one of the Tutorials available in the Monastery; see also Context in perldata) and, in general, RTFM! (Discussion of context should be a significant topic in any decent commercial Perl text or reference book.)


Give a man a fish:  <%-(-(-(-<


In reply to Re^5: unxpected sort warnings while using sort by AnomalousMonk
in thread unxpected sort warnings while using sort by perlynewby

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.