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

Thanks man but for some reason not working - I'm using a .txt file as input to sort BT<W print sort { ($a =~ /\S+\n$/g)[0] cmp ($b =~ /\S+\n$/g)[0] } file.txt not sure if I'm using it correctly

Replies are listed 'Best First'.
Re^3: Sort alphabetically from file
by BillKSmith (Monsignor) on Jun 14, 2019 at 15:54 UTC

    You have just demonstrated why haukex advised you to "post a question effectively". Your problem now has more to do with reading a file than it does with sorting. You must create a filehandle by opening (open) the file and then read from the filehandle with the diamond operatior <> (perlop). All the answers you have received use the special filehandle DATA which is open by default.

    For additional info on sorting, refer to FAQ How do I sort an array by (anything)?

Re^3: Sort alphabetically from file
by hippo (Bishop) on Jun 14, 2019 at 15:12 UTC

    You'll need to open the file first. eg:

    open my $in, '<', 'file.txt' or die "Cannot open file for reading: $!" +; print sort { ($a =~ /\S+\n$/g)[0] cmp ($b =~ /\S+\n$/g)[0] } <$in>; close $in;

    See open and close for all the details.

      This worked - thanks so much :)