http://qs321.pair.com?node_id=11101388


in reply to Re^2: Sort alphabetically from file
in thread Sort alphabetically from file

It is better to open the file using open INFILE, "<", $ARGV[1] or, even better, open $inFILE_HANDLE, "<", $ARGV[1]. It is not causing you a problem now because open() opens for reading (called the "mode") without explicitly setting mode (e.g. reading: "<")

There is a problem reading the file. First you open a file using open(), for that you get a fileHANDLE, e.g. the INFILE or $inFILE_HANDLE. Then you loop reading from the FILE-HANDLE using the diamond operator while(<$inFILE_HANDLE>){ print $_ } and then you close the file(HANDLE): close $inFILE_HANDLE;. You can't read any file contents from a variable which just stores the fileNAME.

In the case your input is not unique wrt the fourth column you will miss input, all similar column-four lines will go to the hash keyed on column-four overriding any previous line with same column-four key. One solution is not to use a hash but an array of arrays (these are formed in exactly the same way as you do now with the regex) and sort works on arrays.

See Re^3: Sort alphabetically from file and Re^3: Sort alphabetically from file, they already gave you hints for file open/read problems.

Replies are listed 'Best First'.
Re^4: Sort alphabetically from file
by LanX (Saint) on Jun 22, 2019 at 23:21 UTC
    > In the case your input is not unique wrt the fourth column you will miss input,

    That's my biggest problem with this hash approach! Thanks for pointing it out. :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice