Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Sort alphabetically from file

by bliako (Monsignor)
on Jun 15, 2019 at 09:29 UTC ( #11101388=note: print w/replies, xml ) Need Help??


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 (Sage) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11101388]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2022-08-08 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?