Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: Sort alphabetically from file

by james28909 (Deacon)
on Jun 15, 2019 at 01:56 UTC ( [id://11101379]=note: print w/replies, xml ) Need Help??

in reply to Sort alphabetically from file

Using Hash of Arrays, is simple enough.
use strict; use warnings; my %hash; while (<DATA> =~ /(\d)\s+(\d)\s+(\d)\s+(\w+)/){ push @{$hash{$4}}, $1, $2, $3; } print "@{$hash{$_}}[0..2] $_\n" for sort keys %hash; __DATA__ 1 2 3 delta 1 2 3 apricot 1 2 3 charlie 1 2 3 bravo 1 2 3 echo 1 2 3 fox
EDIT: changed up code slightly

Replies are listed 'Best First'.
Re^2: Sort alphabetically from file
by edujs7 (Novice) on Jun 15, 2019 at 08:33 UTC

    Original contents:

    thanks I tried the below and no errors but nothing happens, sure it's me though.
    if ($ARGV[0] eq "-a") { open (INFILE, "$ARGV[1]") or die "$ARGV[1] cannot be openned : $!"; my %hash; while ($source_file =~ /(\d)\s+(\d)\s+(\d)\s+(\w+)/) { push @{$hash{$4}}, $1, $2, $3; } print "@{$hash{$_}}[0..2] $_\n" for sort keys %hash; }
    BTW I should've mentioned I must call my program as follows:$ARGV[0]) option($ARGV[1]) mytextfile.txt($ARGV[-1]) I'm using this for assigning the text file to a variable $source_file = "$ARGV[-1] hence using $source_file to call the text file but now working. am I missing something?
    Thanks - will try this out. :)

    2019-06-17 Athanasius restored original contents and added code tags around the program call

      • open (INFILE, "$ARGV[1]") or die "$ARGV[1] cannot be openned : $!"; while ($source_file =~ /(\d)\s+(\d)\s+(\d)\s+(\w+)/)
        Maybe (probably) in your head there's a connection between INFILE and $source_file, but not in your script.
        For Perl, they're unrelated…
      • if ($ARGV[0] eq "-a")
        How do you know your $ARGV[0] really equals "-a"?
        What happens if it doesn't?
        I recommend to add an else block with a print "whatever\n", at least while you're still experimenting.
      • Many of the suggestions to your questions include using strict and warnings. Although this seems to impede or slowing down one's development, I recommend it, too:
        • Yes, the time it takes before your script "runs", will be longer
        • The time until your script works correctly, will be shorter
        One of strict's messages is confusing for beginners:
        Global symbol "$x" requires explicit package name
        should read
        Do you really want "$x" to be a global symbol? Better declare it with my
      If you are new to Perl, you might like diagnostics, which won't throw more errors, but messages that (hopefully) are more informative. So, your script(s) should start with
      use strict; use warnings; use diagnostics;
        noted. Thank you very much for your support.

      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.

        > 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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-06-25 00:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.