in reply to Re^5: Use Perl's Sort to only sort certain lines in a file?
in thread Use Perl's Sort to only sort certain lines in a file?

Doesn't it already sort alphabetically? :)

You'll probably want to read about Schwartzian transform. Anyway, perhaps it will be clearer without pack

last unless $line =~ m{ ( [^(]+ ) # 1 anything except opening paren \s # space \( # opening paren ( \d+ ) # 2 number }x; push @entries, [ $2, $1, $line ]; ... print map { $_->[2] } sort { $a->[0] <=> $b->[0] # sort by $2 || # or if equal $a->[1] cmp $b->[1] # sort by $1 } @entries;
Our @entries is an array of arrays. Each entry (inner array) has: For example, one entry looks like this: ['1', 'wrestling', 'wrestling (1)']

So, we first sort by '1', then by 'wrestling'. Or we can pack '1' and 'wrestling' in just one string and sort by that... that's just a neat trick :)

map simply extracts the line from entry.

Replies are listed 'Best First'.
Re^7: Use Perl's Sort to only sort certain lines in a file?
by grahambuck (Acolyte) on Jan 02, 2015 at 06:03 UTC

    Thank you so much for taking the time to help me with this. Your code has worked perfectly.

    If you'll pardon my one last question (I know, I know, I feel like I can't figure anything out on my own either…), my only experience so far with writing out to a file has been in the form of this:

    open(Input, '+<…') || die "No such file found!"; open(Output, '>…') || die "Can't find the file!"; while(<Input>) { print Output $_; }

    With your code, when I run it I receive a log file with all the data properly formatted. I simply cannot seem to find the right way to then print that out to my Output file.

    If I change print $file; to return $file; I do not receive any output so I know it's stored somewhere.

    There are a few more edits I need to make to the file before writing out as well. Would I write another subroutine, such as "handle_section" and place it within the primary subroutine?

      open(Input, '+<…') || die "No such file found!";
      Well this form of open is considered much worse then
      open my $input, '+<', 'file...';
      First, Input is a global variable and those are generally to be avoided. Second, when you don't specify the mode (here +<) separately, Perl will treat various funny characters (whilespace, |, etc) in filenames in a special way. Which is usually not what you want.
      With your code, when I run it I receive a log file with all the data properly formatted. I simply cannot seem to find the right way to then print that out to my Output file.
      The easiest way would be to use the shell:
      perl script.pl > output.txt
      I'm pretty sure that works with cmd.exe too. Another easy way is to reopen STDOUT (this is where print prints by default - normally the terminal) at the beginning of the program:
      open STDOUT, '>', 'output.txt' or die $!
      There are a few more edits I need to make to the file before writing out as well. Would I write another subroutine, such as "handle_section" and place it within the primary subroutine?
      Sure, that's one way to do it. Another one would be to write one more script and pipe the output of first script to the second (in the shell)
      perl first.pl | perl second.pl
      The first script prints to STDOUT and the second one reads from STDIN:
      while (my $line = <STDIN>) { ... }