in reply to Sort then conditionally sort

The sort function allows you to define your own sorting criteria, as an expression, a block of code or a subroutine.

You can split your records into three fields with something like split(/[\s,]+/). Then all you have to do is decide how to compare the fields.

You can use a lexical comparison (cmp) for the first field and a numeric comparison (<=>) for the second. These operators are described in perlop.

The third field is more challenging and how to proceed depends on details that are not clear to me. You say "each new X value" but not the context in which the value is "new". Are you concerned with the order in which they appear in the input file or the order they appear after sorting the first two fields or something else?

Replies are listed 'Best First'.
Re^2: Sort then conditionally sort
by lukez (Initiate) on Apr 10, 2009 at 16:57 UTC
    Hi Ig, thank you The data in file, is a lot larger and with longer group names between the ''s. this data file is to be only read in and the code will be used to sort it as described in my 1st note. The X-sorted/then cond Y-sorted version is then saved to a new file. The original file is untouched. The 'Groups' can remain in whatever original order they were in, or they can be sorted if that is easier. (doesnt matter). To recap the X's for each 'Group' are sorted Ascending, then for each new X value the direction (ascend/decend) of the Y sort is changed. My other question was the proper way to call in the Data file in PERL, and how to print to another file. I gave my CODE guesses on how to do this, in my other email note. How far off was I on my guesses? Can you fix or confirm the code I thought I would need to do? thanks
    Im sorry to bother you again but I just realized the 'Group' names may contain
    Alpha numeric characters 'aaa3' '5aaa2' '43bbb' etc. Sorry I didnt
    mention this before. Will this affect the type of code sort. By the way these
    groups dont need to be sorted, they can be left in the original order, only
    their XYs need sorting.

      I think you have answers to most of your questions from others by now but briefly...

      Ikegami's update looks good to me.

      There are many ways to do everything - a bit confusing in the beginning but good in the long run. To read your data from another file you can let some perl "magic" do it for you, perhaps something like the following:

      #!/usr/local/bin/perl use strict; use warnings; foreach my $line (<>) { print "$line"; }

      The above script will read every line of every file named on the command line or, if no files are named on the command line, will read from standard input (STDIN). It does nothing but print the contents of the file, but you can put anything you like inside the loop.

      Alternatively, and perhaps a bit less mysteriously, you can open the file explicity yourself. The following would do it:

      #!/usr/local/bin/perl use strict; use warnings; my $filename = shift; # get the filename from the command line open(my $fh, '<', $filename) or die "$filename: $!"; foreach my $line (<$fh>) { print "$line"; }

      You might read open and perlopentut for more on opening files for input and output.

      You may have realized that <DATA> is special: it reads the data in your program file that appears after a line containing "__DATA__" (without the quotes) or "__END__". This is convenient for test scripts and otherwise. You can read more about this in perldata.

      Having some numbers in the group names won't be a problem. If you use Ikegami's examples the group names are sorted lexically. It is easier to sort them so that all the records for a group come together.