Sorting problem... 
Author Message
 Sorting problem...

I'm new to perl and I have a sorting problem that I'm not able to
solve with my limited experience.   I have a directory of files like
so:

april_01_02_shift_h.xls  
march_24_02_shift_j.xls  
march_29_02_shift_i.xls
april_01_02_shift_j.xls  
march_25_02_shift_h.xls  
march_29_02_shift_k.xls
april_02_02_shift_h.xls  
march_25_02_shift_j.xls  
april_03_02_shift_k.xls
march_27_02_shift_i.xls
march_31_02_shift_j.xls

I need to be able to sort these by date, then by shift letter.  I've
tried several different ways to do this with no luck.  If anyone has
any suggestions to point me in the right direction I would be
greatful. I would like to see how people that actually know what they
are doing before I post what I've written.

Thanks.
-dustin



Tue, 21 Sep 2004 06:16:21 GMT  
 Sorting problem...

Quote:

> I'm new to perl and I have a sorting problem that I'm not able to
> solve with my limited experience.   I have a directory of files like
> so:

> april_01_02_shift_h.xls  
> march_24_02_shift_j.xls  
> march_29_02_shift_i.xls
> april_01_02_shift_j.xls  
> march_25_02_shift_h.xls  
> march_29_02_shift_k.xls
> april_02_02_shift_h.xls  
> march_25_02_shift_j.xls  
> april_03_02_shift_k.xls
> march_27_02_shift_i.xls
> march_31_02_shift_j.xls

> I need to be able to sort these by date, then by shift letter.  I've
> tried several different ways to do this with no luck.  If anyone has
> any suggestions to point me in the right direction I would be
> greatful. I would like to see how people that actually know what they
> are doing before I post what I've written.

You could make life easier for yourself in the future by picking easy
to sort names e.g.:

  2002_04_03_shift_z.xls

  yyyy_mm_dd_shift_x

If you want to see what others have done then try the command

  perldoc -q sort

and see if something like this makes any sense:

#!/usr/bin/env perl

use warnings;
use strict;


  april_01_02_shift_h.xls
  march_24_02_shift_j.xls
  march_29_02_shift_i.xls
  april_01_02_shift_j.xls
  march_25_02_shift_h.xls
  march_29_02_shift_k.xls
  april_02_02_shift_h.xls
  march_25_02_shift_j.xls
  april_03_02_shift_k.xls
  march_27_02_shift_i.xls
  march_31_02_shift_j.xls
  /;

my %month = ( 'march' => 3, 'april' => 4 );

print join "\n", map { $_->[0] }
  sort {
    $a->[3] <=> $b->[3]
      || $a->[1] <=> $b->[1]
      || $a->[2] <=> $b->[2]
      || $a->[4] cmp $b->[4]
  }
  map {
    /^([a-z]+)_(\d\d)_(\d\d)_shift_(.)\.xls$/
      ? [ $_, $month{$1}, $2, $3, $4 ]
      : ();

print "\n";

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA



Tue, 21 Sep 2004 07:31:17 GMT  
 Sorting problem...
Thanks mike.   I did some reading and I got a working solution (at
least no bugs yet).  I'm sure I'm doing this the hard way, but the
learning experience was fun.

###########################################################

$datadir = '/opt/mydata';
$serverroot = '/mylogs';
$template = "$datadir/template.html";
$target_line = '<--shift log content here -->';

my %mon = ('January', '1','February', '2','March', '3','April',
'4','May', '5','
June', '6','July', '7','August', '8','September', '9','October',
'10','November'
, '11','December', '12');

y_04_2002_shift_j.xls','decEmBeR_04_1999_shift_x.xls','may_16_1979_shift_c.xls',
'may_16_1979_shift_d.xls','may_16_1979_shift_x.xls');

print "Content-type: text/html\n\n";

opendir(DIRECTORY, $datadir)

closedir DIRECTORY;



my $i = 0;
print "debug unsorted = $#unsorted\n" if $debug;

        if($_ =~ /^(\w+)_(\d+)_(\d\d\d\d)_shift_(\w).[xX][lL][sS]$/) {
                ($day, $year) = ($2, $3);
                $month = ucfirst lc $1;
                $shift = uc $4;

                $record[$i] = {};
                $record[$i]{month} = $month;
                $record[$i]{month_num} = $mon{$month};
                $record[$i]{day} = sprintf "%02d", $day;
                $record[$i]{year} = $year;
                $record[$i]{shift} = $shift;
                $record[$i]{filename} = $_;
                $i++;
        }

Quote:
}


        $b->{year} <=> $a->{year}
        ||
        $b->{month_num} <=> $a->{month_num}
        ||
        $b->{day} <=> $a->{day}
        ||
        $a->{shift} cmp $b->{shift}


print "debug: record postsort = $#record\n" if $debug;
for($j = 0; $j < $#record+1; $j++) {

href=\"$serverroot/$record[$j]{filename}\">$record[$j
]{day}-$record[$j]{month}-$record[$j]{year} Shift
$record[$j]{shift}</a><br>\n";
Quote:
}

print "debug: dirlist = $#dirlist\n" if $debug;
open TEMPLATEFILE, "$template"
        or die "Can't open template file: $template\n";
while (<TEMPLATEFILE>) {
        chomp;

        print "$_\n";
Quote:
}

close TEMPLATEFILE;

exit 0;


Tue, 21 Sep 2004 13:27:10 GMT  
 Sorting problem...

Quote:
> march_25_02_shift_j.xls
> april_03_02_shift_k.xls
> march_27_02_shift_i.xls
> march_31_02_shift_j.xls

> I need to be able to sort these by date, then by shift letter.  I've

Something along these lines should do the job (not trying to optimize here
but to keep it somewhat readible):

sub mycmp{


    if ($a_array[2] <=> $b_array[2]) { return $a_array[2] <=> $b_array[2];};
    if ($a_array[1] <=> $b_array[1]) { return $a_array[1] <=> $b_array[1];};
    if ($a_array[4] cmp $b_array[4]) { return $a_array[4] cmp $b_array[4];};
 }

Of course this is not very fast because the split will be done over and over
again.
If you want to avoid this you should look into the Schwartzian
Transformation (please see google for details)

jue



Wed, 22 Sep 2004 08:55:53 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Sort of a sort problem.

2. Sorting problem

3. list sorting problem, please help

4. FormMail sort problem w/realname,email

5. sorting problem

6. Simple? sort problem

7. Funky sorting problem!!

8. sorting problem

9. Sorting problem -New to Perl

10. sorting problem

11. Sort Problem

12. sort problem

 

 
Powered by phpBB® Forum Software