in reply to Re: Sort then conditionally sort
in thread Sort then conditionally sort

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.

Replies are listed 'Best First'.
Re^3: Sort then conditionally sort
by ig (Vicar) on Apr 16, 2009 at 02:34 UTC

    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.