Numerical sort (Schwartzian xform)

Discussion in 'Perl Misc' started by Jorge, Sep 6, 2006.

  1. Jorge

    Jorge Guest

    I'm running ActiveState perl 5.8.8 on XP

    While working on my dog racing program, I build an array containing
    these strings which are numerically indexed 1 thru 8 by the last
    column.

    31.43 32 1005.89 Figs Domino 1
    31.62 17 537.57 Boc's Skater 2
    31.85 18 573.34 Reward Crystal 3
    31.54 27 851.47 Db Miss Buxley 4
    31.78 13 413.17 Ucme Lola 5
    31.96 12 383.46 Bayou Jasmine 6
    31.62 14 442.62 Y's Flirt 7
    31.46 30 943.88 Sheriff Wade 8

    I pass the array to my schwartzian transform sub routine using this
    call ...

    my @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);

    with expectations of it returning an array that is sorted on the
    numerical 1st column (2-place floats) but it always returns the
    original array. I couldn't find anything in perlfaq4 that explains this
    nor could I find anything on the web that explained it in terms I could
    understand, notwithstanding the tonnes of articles about Perl sorting.

    Can anyone give me an idea how to get the desired behaviour on this?
    Your help is greatly appreciated - TIA.

    Here is my SchwartzianTransform sub routine:

    sub SchwartzianTransform{
    my ($sep, $cols);
    if (ref $_[0]) {
    $sep = '\\s+'
    } else {
    $sep = shift;
    }
    unless (ref($cols = shift) eq 'ARRAY') {
    die "fieldsort columns must be in anon array";
    }
    my (@sortcode, @col);
    my $col = 1;
    for (@$cols) {
    my ($a, $b) = /^-/ ? qw(b a) : qw(a b);
    my $op = /n$/ ? '<=>' : 'cmp';
    push @col, (/(\d+)/)[0] - 1;
    push @sortcode, "\$${a}->[$col] $op \$${b}->[$col]";
    $col++;
    }
    my $sortfunc = eval "sub { " . join (" or ", @sortcode) . " } ";
    my $splitfunc = eval 'sub { (split /$sep/o, $_)[@col] } ';
    return
    map $_->[0],
    sort { $sortfunc->() }
    map [$_, $splitfunc->($_)],
    @_;
    }
     
    Jorge, Sep 6, 2006
    #1
    1. Advertising

  2. Jorge

    Guest

    Jorge wrote:
    > my @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);
    >
    > with expectations of it returning an array that is sorted on the
    > numerical 1st column (2-place floats)


    waitaminute - you want to sort an array on the first column? Why not
    just sort the array:
    sort @array

    If your data is consistent (^\d\d\.\d\d) this is all you need to do.

    --
    David Filmer (http//DavidFilmer.com)
     
    , Sep 6, 2006
    #2
    1. Advertising

  3. Jorge

    Ala Qumsieh Guest

    wrote:

    > Jorge wrote:
    >> my @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);
    >>
    >> with expectations of it returning an array that is sorted on the
    >> numerical 1st column (2-place floats)

    >
    > waitaminute - you want to sort an array on the first column? Why not
    > just sort the array:
    > sort @array
    >
    > If your data is consistent (^\d\d\.\d\d) this is all you need to do.


    It is safer to not make such an assumption, and the solution isn't much more
    complicated anyway:

    sort {$a <=> $b} @array;

    --Ala
     
    Ala Qumsieh, Sep 6, 2006
    #3
  4. Jorge

    Jorge Guest

    A straight sort was my first attempt ...

    @sorted_array = sort @array;

    foreach $line(@sorted_array){print "$line";}

    and it produces the original unsorted array so I thought possibly the
    problem had something to do with how sorting works (ascii, alpha,
    numerical, lexical, whatever) and that I had to point it to the first
    column and ask for a numerical sort ... hence my attempt at the
    schwartzian transform.

    wrote:
    > Jorge wrote:
    > > my @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);
    > >
    > > with expectations of it returning an array that is sorted on the
    > > numerical 1st column (2-place floats)

    >
    > waitaminute - you want to sort an array on the first column? Why not
    > just sort the array:
    > sort @array
    >
    > If your data is consistent (^\d\d\.\d\d) this is all you need to do.
    >
    > --
    > David Filmer (http//DavidFilmer.com)
     
    Jorge, Sep 6, 2006
    #4
  5. Jorge

    -berlin.de Guest

    Jorge <> wrote in comp.lang.perl.misc:
    > I'm running ActiveState perl 5.8.8 on XP
    >
    > While working on my dog racing program, I build an array containing
    > these strings which are numerically indexed 1 thru 8 by the last
    > column.
    >
    > 31.43 32 1005.89 Figs Domino 1
    > 31.62 17 537.57 Boc's Skater 2
    > 31.85 18 573.34 Reward Crystal 3
    > 31.54 27 851.47 Db Miss Buxley 4
    > 31.78 13 413.17 Ucme Lola 5
    > 31.96 12 383.46 Bayou Jasmine 6
    > 31.62 14 442.62 Y's Flirt 7
    > 31.46 30 943.88 Sheriff Wade 8
    >
    > I pass the array to my schwartzian transform sub routine using this
    > call ...
    >
    > my @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);
    >
    > with expectations of it returning an array that is sorted on the
    > numerical 1st column (2-place floats) but it always returns the
    > original array.


    For me it returns the sorted array. Show a complete, runnable
    program that shows the behavior you are seeing.

    Anno
     
    -berlin.de, Sep 6, 2006
    #5
  6. Jorge

    Jorge Guest

    -berlin.de wrote:
    > Jorge <> wrote in comp.lang.perl.misc:
    > > I'm running ActiveState perl 5.8.8 on XP
    > >
    > > While working on my dog racing program, I build an array containing
    > > these strings which are numerically indexed 1 thru 8 by the last
    > > column.
    > >
    > > 31.43 32 1005.89 Figs Domino 1
    > > 31.62 17 537.57 Boc's Skater 2
    > > 31.85 18 573.34 Reward Crystal 3
    > > 31.54 27 851.47 Db Miss Buxley 4
    > > 31.78 13 413.17 Ucme Lola 5
    > > 31.96 12 383.46 Bayou Jasmine 6
    > > 31.62 14 442.62 Y's Flirt 7
    > > 31.46 30 943.88 Sheriff Wade 8
    > >
    > > I pass the array to my schwartzian transform sub routine using this
    > > call ...
    > >
    > > my @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);
    > >
    > > with expectations of it returning an array that is sorted on the
    > > numerical 1st column (2-place floats) but it always returns the
    > > original array.

    >
    > For me it returns the sorted array. Show a complete, runnable
    > program that shows the behavior you are seeing.
    >
    > Anno


    Here is the program which requires a dedicated directory named
    c:\dogs\tonight which contains the data file (at the end of the post --
    truncated to save bandwidth).

    snip<------ program starts here ------>snip

    #!perl -lw

    use warnings;
    use strict;
    use File::path;

    &createIndividualDogsFiles("c:\\dogs\\tonight");

    sub createIndividualDogsFiles{
    my(@filesArr, $filesArr, $dir, $path, $total, $totalCnt, $avg);
    my($phx, $day, $mon, $dat, $yr, $matEve, $Race, $raceNum, $Grade);
    my($gradeLet, $Dist, $distNum, $race, $numOfRaces, $racesAdded, $ans);
    my($vaultFile, $raceArr, $dogName, $todaysPostNum, $finalTime,
    $field1);
    my $flag = 0;
    my @resultsArr = ();
    my $weight;
    my $pp;
    my($junk1, $junk2, $junk3);
    my(@array, $array, $string, @sorted_array, $line);

    $dir = shift;
    unless (opendir(DIR, $dir)) {
    warn "Can't open $dir\n";
    closedir(DIR);
    return;
    }
    foreach (readdir(DIR)) {
    next if $_ eq '.' || $_ eq '..';
    $path = "$dir\\$_";
    if (-d $path) { # a directory
    &createIndividualDogsFiles($path);
    } elsif ( -f _) { # a plain file
    open(IN, "$path") || die "Can't open $path for read: $!";
    while(<IN>){
    next if /^\s*$/ || /______/ || /======/ || / Owner / || / Trn /;
    s/\s+\Z//;
    if($_ =~ /Rosnet\,/ && $_ =~ /Race [0-9]/){
    ($junk1, $junk2, $junk3, $phx, $day, $mon, $dat, $yr, $matEve,
    $Race,
    $raceNum, $Grade, $gradeLet, $Dist, $distNum) = split(' ', $_,
    14);
    $race = "$Race"."_"."$raceNum"."_"."$gradeLet";
    print "\n$race\n================";
    }
    elsif($_ !~ /Rosnet\,/ && $_ =~ /Race [0-9]/){
    ($phx, $day, $mon, $dat, $yr, $matEve, $Race, $raceNum,
    $Grade, $gradeLet, $Dist, $distNum) = split(' ', $_, 12);
    $race = "$Race"."_"."$raceNum"."_"."$gradeLet";
    print "\n$race\n================";
    }
    elsif($_ =~ /Ken / && $_ =~ / Grade /){
    $total = 0; $totalCnt = 0; $avg = 0;
    $todaysPostNum = substr($_, 0, 1);
    $dogName = substr($_, 2, 18);
    $vaultFile = "c:\\dogs\\vault\\$dogName";

    open(VDOG, "$vaultFile") || die "Can't open $vaultFile for
    reading: $!";
    while(<VDOG>){
    my @stats = split(' ', $_);
    my $fraction = $stats[1];
    $total += $fraction;
    ++$totalCnt;
    }
    close(VDOG);
    if($totalCnt > 0){
    $avg = $total / $totalCnt;
    $string = sprintf("%.2f %-3s%-8s%-20s%-2s", $avg, $totalCnt,
    $total, $dogName, $todaysPostNum);
    push(@array, $string);
    }else{
    $string = "$todaysPostNum $dogName --- No Data ---";
    push(@array, $string);
    }
    }
    @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);
    foreach $line(@sorted_array){
    print "$line";
    }
    @array = ();
    @sorted_array = ();
    }
    close(IN);
    }
    }
    closedir(DIR);
    }

    #================================
    # SUBROUTINE SchwartzianTransform
    #================================

    sub SchwartzianTransform{
    my ($sep, $cols);
    if (ref $_[0]) {
    $sep = '\\s+'
    } else {
    $sep = shift;
    }
    unless (ref($cols = shift) eq 'ARRAY') {
    die "fieldsort columns must be in anon array";
    }
    my (@sortcode, @col);
    my $col = 1;
    for (@$cols) {
    my ($a, $b) = /^-/ ? qw(b a) : qw(a b);
    my $op = /n$/ ? '<=>' : 'cmp';
    push @col, (/(\d+)/)[0] - 1;
    push @sortcode, "\$${a}->[$col] $op \$${b}->[$col]";
    $col++;
    }
    my $sortfunc = eval "sub { " . join (" or ", @sortcode) . " } ";
    my $splitfunc = eval 'sub { (split /$sep/o, $_)[@col] } ';
    return
    map $_->[0],
    sort { $sortfunc->() }
    map [$_, $splitfunc->($_)],
    @_;
    }

    __END__

    snip<----- data file ------>snip

    Rosnet,Inc - Greyhound RacingPhoenix Tuesday Sep
    05 2006 Evening Race 1 Grade D Dist 550

    1 Figs Domino Ken Olsker Kennel Corp PH 44 5 8 4
    5 Hi Grade - A Lo Grade - D
    Trn Tim Olsker PH 9 3 1 0
    2 Best Time 30.85
    Black M 6/5/2004 Courageous Nicky Figs Cher Owner Dick Figurilli

    PH09-01-06E 03 550 F 31.07 77½ 5 2 2 3 3 5 31.44 05.30 D
    Bumped 1st Turn GecMartin Gablicious Chsm'sIndy 8
    PH08-27-06E 13 550 F 31.58 77 5 7 4 4 4 8 32.13 07.80 D
    Evenly, Inside Rd'sQuenAn GlsRustler Boc'sAttid 8
    PH08-21-06E 13 550 F 31.82 76½ 1 1 1 2 1 2 2 1½ 31.92 05.30 D
    Outfinished, Inside WntsAndNed KmaDiesel RwardFrsty 8
    PH08-15-06E 03 550 F 31.16 77 3 6 4 5 6 10 31.85 27.60 C
    Wide In Stretches KayFrbidng UsGoldenRl PatCPayabl 8
    PH08-09-06E 11 550 S 31.75 78 3 4 6 6 6 14 32.77 21.50 C
    Bumped 1st Turn CstarDesrt He'sOnThRn Flea'sCuti 8
    PH08-04-06E 15 550 F 30.81 77 7 5 4 4 6 14 31.79 12.30 C
    Offstride Far Turn StatUSDarD Bc'sLilNck AngelcBaty 7
    _________________________________________________________________________________________________________________________
    2 Boc's Skater Ken Bret Pachello PH 18 1 1 2
    5 Hi Grade - B Lo Grade - D
    Trn Michael Robinette PH 0 0 0 0
    0 Best Time 31.05
    Brindle M 3/12/2003 Trojan Cruze Contradiction Owner Brad Boeckenstedt

    PH09-01-06E 01 550 F 31.19 75½ 6 2 2 4 8 16 32.35 12.40 D
    Bumped Homestretch Absolm KmaKatnamo TvDamndbck 8
    PH08-28-06S 07 550 F 31.86 75½ 7 4 2 3 5 12 32.74 ----- SD
    Faded Far Turn Rlm'sBrwsr Boc'sMvnOn CldwaterIr 6
    PH08-24-06S 08 550 F 31.6 77 1 2 1 hd 1 ½ 3 5 31.96 ----- SD
    Outfinished, Inside Kaynta DjaysBlstO Boc'sBluBy 6
    PH06-26-06E 01 550 F 31.37 74 4 6 5 5 5 8 31.91 11.50 D
    Never Prominent Rj'sAntiEm BcAllstar MaraTheSam 7
    PH06-18-06E 09 550 F 31 74½ 3 1 5 7 7 14 32.03 19.30 D
    Early Threat, Faded LowFlynRck RagingSurf FigsDomino 8
    PH06-11-06E 03 550 F 30.77 74½ 3 2 5 7 8 28 32.74 10.30 D
    Collided 1st Turn Rd'sUndo UsLateShow TuffyTitan 8
    _________________________________________________________________________________________________________________________
    3 Reward Crystal Ken Gloria Dorsey PH 20 4 1 1
    1 Hi Grade - C Lo Grade - M
    Trn Patrick Phillips 0 0 0 0
    0 Best Time 30.94
    Dark Brindle F 9/13/2004 Wigwam Hoss Hkw Pongacrystal Owner Dr. Carl
    E. Ward
    PH08-31-06E 09 550 F 31.06 59½ 4 4 5 7 8 10 31.78 10.20 D
    Steady Fade KrazyAsKat AmfAdroit JupiterTym 8
    PH08-27-06E 07 550 F 31.65 59 2 3 2 4 6 10 32.34 04.90 D
    Steady Fade ThinkMore UcmeKimmi Auster 8
    PH08-21-06E 03 550 F 31.66 60 7 3 2 6 7 11 32.44 04.40 D
    Crowded Backstr & Fa Wayn'sBbyd KaycarDshr FgsSmallBs 8
    PH08-17-06E 01 550 F 31.21 60 6 2 2 2 2 5½ 31.59 12.90 D
    Chased Winner, Insid GpherChcks BcGeeHoney KmWhodoVoo 8
    PH08-12-06E 13 550 F 31.15 59½ 6 5 5 6 8 12 32.04 10.90 C
    Never A Threat GlsRedalrt RsSammy HeyKnocker 8
    PH08-06-06E 15 550 S 31.54 59 8 2 2 2 4 9 32.15 22.70 C
    Weakened Hmstrch, In CldwterThe FigsMona GrysSnKing 8
    _________________________________________________________________________________________________________________________
    4 Db Miss Buxley Ken Olsker Kennel Corp PH 35 1 8 5
    9 Hi Grade - C Lo Grade - J
    Trn Tim Olsker PH 60 5 8 5
    7 Best Time 31.13
    White Black Bd. F 7/6/2003 Craigie Glen Db Cinema Owner Duran Bros.

    PH09-01-06E 17 550 F 31.38 54½ 1 1 5 4 4 10 32.11 05.20 C
    Bumped 1st Turn FlthyMcNst DafodlDbrh SavvyLouis 8
    PH08-27-06E 11 550 F 31.29 54 8 8 8 8 8 17 32.49 11.60 C
    Bumped 1st Turn Kane'sRchl Brilntcrnr Rlm'sLardo 8
    PH08-23-06E 11 550 F 31.62 54½ 6 2 2 3 4 6 32.06 05.30 C
    Outfinished, Inside NatashaLyn I'llTwThen DafodlDbrh 8
    PH08-19-06E 17 550 S 32.08 54 3 7 4 5 7 11 32.92 08.10 C
    Some Fade CruzenWili Brilntcrnr NatashaLyn 8
    PH08-14-06E 02 550 F 31.56 55½ 2 1 2 2 2 ½ 31.61 07.00 C
    Almost Up, Inside RagingSurf BellaAwacs Brilntcrnr 8
    PH08-09-06E 09 550 S 31.98 55 2 2 3 6 8 10 32.73 04.50 C
    Early Threat, Faded BestBetsy Rd'sNabsco RagingSurf 8
    _________________________________________________________________________________________________________________________
    5 Ucme Lola Ken Fidel Or Silvia SambadePH 24 1 4 3
    4 Hi Grade - C Lo Grade - J
    Trn Willie Davis 0 0 0 0
    0 Best Time 31.05
    Red F 2/19/2004 Ww Night Rider Passion Plan Owner Sherry De Witt Or
    Shelly Rangel
    PH08-31-06E 05 550 F 31.4 61½ 2 6 5 5 6 13 32.37 04.50 D
    Offstr 1st Tn - Blck StatUSJyze CldwatrKck HindSight 8
    PH08-25-06E 01 550 F 31.45 61½ 3 7 8 7 8 19 32.84 05.90 D
    Bumped 1st & Homestr OddMonica AcMyPrsasn IrskaRedMg 8
    PH08-20-06E 09 550 F 31.6 60 5 7 4 3 2 4 31.89 *03.50 D
    Offstride Early Beemr Maybemybnt Twistedmnt 8
    PH08-15-06E 09 550 F 30.75 60 5 5 3 2 2 10 31.52 06.20 D
    Next Best, Midtrack Cherryprsp Boc'sSnoke MoonMtCrky 8
    PH08-09-06E 01 550 S 31.42 60 7 4 3 2 2 4½ 31.73 14.00 D
    Followed The Pace, M WhiskeyWhs CajunVal SweetAzArz 7
    PH08-05-06E 07 550 S 32.26 60½ 4 5 5 3 4 12 33.14 06.50 D
    Bumped 1st Turn FgsArmstrn NotAzChocl Hj'sTyler 8
    _________________________________________________________________________________________________________________________
    6 Bayou Jasmine Ken George Fune PH 18 1 0 2
    5 Hi Grade - D Lo Grade - J
    Trn Dorothy Fune 0 0 0 0
    0 Best Time 31.62
    Brindle F 4/3/2004 Oshkosh Slammer Mesa Greatone Owner Van Strother

    PH09-01-06E 07 550 F 31.25 58 5 8 5 5 5 7 31.76 15.50 D
    Bumped 1st Turn Auster SherifWade FgsSmallBs 8
    PH08-28-06E 11 550 F 31.36 57 2 7 8 8 8 13 32.32 42.90 D
    Bumped 1st Turn UsValeyFrg SltaryChrg See URound 8
    PH08-24-06E 07 550 F 30.9 57½ 1 7 8 7 7 20 32.35 07.80 D
    Always Back GecHottie UsValeyFrg DutchLilAn 8
    PH08-18-06E 03 550 F 31.26 58 5 8 6 6 4 9 31.88 05.30 D
    Sht Off Erly - Frcd CstarTaran FgsDarkKal UcmeJoe 8
    PH08-12-06E 05 550 F 31.62 57½ 5 6 4 2 1 1½ 31.62 15.40 J
    Drove To Win, Midtra ChicagMcky UsLandRovr BcBillyBoy 8
    PH08-03-06E 05 550 F 31.21 57 7 6 5 5 3 8½ 31.81 21.10 J
    Bumped 1st Turn SpicyDream BubleGmFev KayCeeSasn 7
    _________________________________________________________________________________________________________________________
    7 Y's Flirt Ken Fidel Or Silvia SambadePH 18 3 1 2
    2 Hi Grade - C Lo Grade - M
    Trn Willie Davis 0 0 0 0
    0 Best Time 31.03
    Black M 7/4/2004 Oshkosh Slammer Tm's Merry Pace Owner Silvia Sambade

    PH08-26-06E 07 550 F 31.81 74 6 5 6 6 6 10 32.51 08.70 D
    Bumped 1st Turn Rd'sVictor Fla'sHiway IrskaMyrtl 8
    PH08-17-06E 01 550 F 31.21 74 1 6 5 5 7 11 31.99 05.30 D
    Offstride 1st & Far GpherChcks RwardCryst BcGeeHoney 8
    PH08-11-06E 17 550 M 31.17 75 6 4 5 5 7 22 32.75 09.10 C
    Crowded Far Turn ClSpeedie I'llTwThen Rd'sKates 7
    PH08-07-06E 14 550 S 31.47 73 6 6 4 4 6 13 32.43 12.70 C
    Late Fade SlitaryAsh GecHenry RewrdRedGr 7
    PH08-03-06E 09 550 F 31.1 73½ 3 6 7 8 8 16 32.24 14.60 C
    Bumped 1st Turn DtchDaniel RagingSurf RsSammy 8
    PH07-29-06E 17 550 F 30.5 73½ 1 5 5 6 4 16 31.62 03.50 C
    Varied Little, Insid AndrwsPstc UssVentura FigsIou 8
    _________________________________________________________________________________________________________________________
    8 Sheriff Wade Ken Lonnie Boyle PH 34 4 6 1
    7 Hi Grade - B Lo Grade - D
    Trn Jamie Boyle PH 18 1 3 4
    5 Best Time 30.79
    Black M 3/13/2004 Scorcher's Ace Dawn's Earlylite Owner Howard L.
    Marshall
    PH09-01-06E 07 550 F 31.25 76½ 2 2 2 2 2 2 31.38 07.70 D
    Followed The Pace, I Auster FgsSmallBs UcmeKimmi 8
    PH08-27-06E 01 550 F 31.73 77½ 7 8 8 8 8 10 32.42 21.50 D
    Offstride 1st Turn LitlLisaLo HindSight IvansImage 8
    PH08-22-06E 09 550 F 31.42 75 4 2 5 5 8 14 32.44 08.10 D
    Dropped Back Early BcGeeHoney MoonMtBomr KmaKatnamo 8
    PH08-17-06S 06 550 F 31.27 76½ 6 2 4 4 4 10 31.99 ----- SD
    Slight Factor, Insid GecMartin Vl ColdwterDn 7
    PH07-30-06E 13 550 F 30.99 76½ 1 3 5 5 5 7½ 31.52 03.40 D
    Never Prominent FigsMona Insider'sG QalityChic 8
    PH07-24-06E 01 550 F 31.02 77 4 6 5 4 4 6 31.45 03.40 D
    Bumped At Break & Ea FgsconchCr CldwatrKck ImaKtyDdgr 8
    _________________________________________________________________________________________________________________________
    =========================================================================================================================
     
    Jorge, Sep 6, 2006
    #6
  7. Jorge

    Jorge Guest

    Addition to my previous post ...

    The program also has a dependency on a library of data (vault
    directory) which I built with another program and the library is simply
    too large to post ... ~ 1600 files. The program will not successfully
    run without the library.
     
    Jorge, Sep 6, 2006
    #7
  8. Jorge

    John Bokma Guest

    "Jorge" <> wrote:

    > Addition to my previous post ...
    >
    > The program also has a dependency on a library of data (vault
    > directory) which I built with another program and the library is simply
    > too large to post ... ~ 1600 files. The program will not successfully
    > run without the library.


    In which case it's a good idea to reduce your program to the smallest
    version that shows your problem.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Sep 6, 2006
    #8
  9. Jorge

    Jorge Guest

    Good advice -- thank you ...

    I pruned the program to this ...

    #!perl -lw

    use strict;

    my(@array, @sorted, $line);

    open(IN, "infile.txt") || die "Can't open infile for read: $!";
    while(<IN>){
    push(@array, $_);
    }
    close(IN);

    @sorted = sort @array;

    foreach $line(@sorted){
    print "$line";
    }

    __END__

    which reads this file ...

    31.43 32 1005.89 Figs Domino 1
    31.62 17 537.57 Boc's Skater 2
    31.85 18 573.34 Reward Crystal 3
    31.54 27 851.47 Db Miss Buxley 4
    31.78 13 413.17 Ucme Lola 5
    31.96 12 383.46 Bayou Jasmine 6
    31.62 14 442.62 Y's Flirt 7
    31.46 30 943.88 Sheriff Wade 8

    to create the array and it indeed does sort properly jusing the
    standard sort() function.

    Obviously, when I create the array on the fly in the program, I am
    somehow bringing to the surface something that precludes sorting (for
    whatever reason) maybe it's a whitespace problem or a seperator problem
    or ...

    I'll dig around until I find it.

    Thanks



    John Bokma wrote:
    > "Jorge" <> wrote:
    >
    > > Addition to my previous post ...
    > >
    > > The program also has a dependency on a library of data (vault
    > > directory) which I built with another program and the library is simply
    > > too large to post ... ~ 1600 files. The program will not successfully
    > > run without the library.

    >
    > In which case it's a good idea to reduce your program to the smallest
    > version that shows your problem.
    >
    > --
    > John Experienced Perl programmer: http://castleamber.com/
    >
    > Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    Jorge, Sep 6, 2006
    #9
  10. >>>>> "Jorge" == Jorge <> writes:

    Jorge> I pass the array to my schwartzian transform sub routine using this
    Jorge> call ...

    Jorge> my @sorted_array = &SchwartzianTransform(' ', ['1n', -1], @array);

    You don't like Sort::Fields (in the CPAN) for some reason?

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Randal L. Schwartz, Sep 7, 2006
    #10
  11. Jorge <> wrote:

    > Good advice



    What is good advice?


    > while(<IN>){
    > push(@array, $_);
    > }



    This does the same thing:

    my @array = <IN>;


    > @sorted = sort @array;



    This does the same thing, only without the unnecessary temporary variable:

    my @sorted = sort <IN>;



    > foreach $line(@sorted){
    > print "$line";
    > }



    perldoc -q vars

    What's wrong with always quoting "$vars"?


    This does the same thing:

    print @sorted;



    Do you get paid by the line or something?



    [ snip TOFU. Please stop doing that! ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 7, 2006
    #11
  12. Jorge

    John Bokma Guest

    (Randal L. Schwartz) wrote:

    >>>>>> "Jorge" == Jorge <> writes:

    >
    > Jorge> I pass the array to my schwartzian transform sub routine using
    > this Jorge> call ...
    >
    > Jorge> my @sorted_array = &SchwartzianTransform(' ', ['1n', -1],
    > @array);
    >
    > You don't like Sort::Fields (in the CPAN) for some reason?


    Doesn't have your last name in the module name (or any of the methods [1])
    :-D

    [1] wild guess.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Sep 7, 2006
    #12
  13. Jorge

    Uri Guttman Guest

    >>>>> "JB" == John Bokma <> writes:

    JB> (Randal L. Schwartz) wrote:
    >>>>>>> "Jorge" == Jorge <> writes:

    >>

    Jorge> I pass the array to my schwartzian transform sub routine using
    >> this Jorge> call ...
    >>

    Jorge> my @sorted_array = &SchwartzianTransform(' ', ['1n', -1],
    >> @array);
    >>
    >> You don't like Sort::Fields (in the CPAN) for some reason?


    JB> Doesn't have your last name in the module name (or any of the
    JB> methods [1]) :-D

    neither does sort::maker. it uses ST for the key to select that sort
    style. same for the GRT. :)

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Sep 7, 2006
    #13
  14. Jorge

    Jorge Guest

    Never heard of Sort::Fields (until now :D) so I just did a ppm install
    and will give it a try.

    Meanwhile, thanks for all the replies.

    Jorge

    Uri Guttman wrote:
    > >>>>> "JB" == John Bokma <> writes:

    >
    > JB> (Randal L. Schwartz) wrote:
    > >>>>>>> "Jorge" == Jorge <> writes:
    > >>

    > Jorge> I pass the array to my schwartzian transform sub routine using
    > >> this Jorge> call ...
    > >>

    > Jorge> my @sorted_array = &SchwartzianTransform(' ', ['1n', -1],
    > >> @array);
    > >>
    > >> You don't like Sort::Fields (in the CPAN) for some reason?

    >
    > JB> Doesn't have your last name in the module name (or any of the
    > JB> methods [1]) :-D
    >
    > neither does sort::maker. it uses ST for the key to select that sort
    > style. same for the GRT. :)
    >
    > uri
    >
    > --
    > Uri Guttman ------ -------- http://www.stemsystems.com
    > --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    > Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Jorge, Sep 7, 2006
    #14
  15. Jorge

    Jorge Guest

    The good advice was suggesting I reduce my program to something that is
    managable for debugging -- which I did and it helped greatly.

    Do I get paid by the line??? LOL -- actually I'm retired and Perl is a
    necessity that supports my hobbies but I suppose if I programmed Perl
    for a living, my Dr. Strangelove side would come out and I'd stop
    worrying and learn how to love the one-liners. :)

    Actually, your tips and style points are noted and appreciated and will
    find themselves being used as I get used to the syntax.

    have a good one ...

    Jorge



    Tad McClellan wrote:
    > Jorge <> wrote:
    >
    > > Good advice

    >
    >
    > What is good advice?
    >
    >
    > > while(<IN>){
    > > push(@array, $_);
    > > }

    >
    >
    > This does the same thing:
    >
    > my @array = <IN>;
    >
    >
    > > @sorted = sort @array;

    >
    >
    > This does the same thing, only without the unnecessary temporary variable:
    >
    > my @sorted = sort <IN>;
    >
    >
    >
    > > foreach $line(@sorted){
    > > print "$line";
    > > }

    >
    >
    > perldoc -q vars
    >
    > What's wrong with always quoting "$vars"?
    >
    >
    > This does the same thing:
    >
    > print @sorted;
    >
    >
    >
    > Do you get paid by the line or something?
    >
    >
    >
    > [ snip TOFU. Please stop doing that! ]
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
     
    Jorge, Sep 8, 2006
    #15
  16. Jorge

    John Bokma Guest

    "Jorge" <> wrote:

    > Actually, your tips and style points are noted and appreciated and
    > will find themselves being used as I get used to the syntax.


    Final tip, and "we" suggest to read it carefully:
    <http://johnbokma.com/mexit/2006/04/11/how-to-reply.html>

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Sep 8, 2006
    #16
  17. Jorge <> wrote:

    > The good advice was suggesting I reduce my program to something that is
    > managable for debugging --



    Then your comment that it was good advice should have come after
    quoted text containing that good advice.

    Have you seen the Posting Guidelines that are posted here frequently?


    > which I did and it helped greatly.



    Have you seen the Posting Guidelines that are posted here frequently?

    They suggest reducing the program too.



    >> [ snip TOFU. Please stop doing that! ]



    [ snip yet more TOFU. Please stop doing that before it is too late! ]


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 8, 2006
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard R.
    Replies:
    1
    Views:
    633
    Martin Honnen
    Nov 8, 2004
  2. xform novice help

    , Dec 11, 2006, in forum: XML
    Replies:
    1
    Views:
    422
    Martin Honnen
    Dec 12, 2006
  3. David Di Biase

    Re: Schwartzian transform for tuple in list

    David Di Biase, Sep 24, 2008, in forum: Python
    Replies:
    5
    Views:
    262
    Steven D'Aprano
    Sep 25, 2008
  4. Madjid Nasiri

    XForm and WebService

    Madjid Nasiri, Oct 20, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    127
    Madjid Nasiri
    Oct 20, 2004
  5. William James

    Schwartzian Transform built-in to Perl 6?

    William James, Aug 28, 2005, in forum: Perl Misc
    Replies:
    0
    Views:
    106
    William James
    Aug 28, 2005
Loading...

Share This Page