in reply to Re^4: unxpected sort warnings while using sort
in thread unxpected sort warnings while using sort
... 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: <%-(-(-(-<
|
|---|