lakeTrout has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks,

I'm trying to make a table of contents of sorts from a flatFile. I would like to sort the array so the list is in alphabetical order, but I seem to be struggling with what I'm sure is a very simple problem. I've tried sort(@fields) and fields = sort(fields) and don't seem to get anywhere. Can anyone help me out with what I'm missing?
open (DB,"logfile.log") or die("There was a problem opening the file." +); while ($entry=<DB>) { @fields=split(/\t/,$entry); chomp @fields; print <<ENDHTML; <li><a href="#$fields[0]">$fields[0] ( $fields[1] )</a></li> ENDHTML } close DB;

Thank You!

LakeTrout

Replies are listed 'Best First'.
Re: sort after split
by dragonchild (Archbishop) on Dec 05, 2007 at 19:57 UTC
    @fields = sort { $a cmp $b } split /\t/, $entry;

    sort


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Thanks Dragonchild,
      I read that in perldoc, however, my output is just the parens, like so:
      • ()
      • ()
      • ()
      Any idea why? Thanks!

      Thanks!
        Check to make sure $entry has what you think it does. That sounds like $entry is "\t\t" (or somesuch). Try adding the -w flag.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: sort after split
by ysth (Canon) on Dec 06, 2007 at 05:08 UTC
    It sounds like you what you've tried is sorting the fields for each logfile.log record, when what it sounds like to want to do is sort the records themselves.

    To do that, you need to save them all and then sort them, something like this:

    while (my $entry = <DB>) { chomp(my @fields = split(/\t/, $entry)); push @records, \@fields; } @records = sort { $a->[1] cmp $b->[1] } @records; for my $fields (@records) { print <<ENDHTML; <li><a href="#$fields->[0]">$fields->[0] ($fields->[1])</a></li> ENDHTML }
    (untested, and making some guesses about what you want)
      BINGO! Thanks ysth!

      After some minor tweaking I have it working beautifully now. Thanks to you and everyone else. have a good day!

      LakeTrout
Re: sort after split
by Roy Johnson (Monsignor) on Dec 05, 2007 at 20:41 UTC
    What does your data look like? Give a few sample lines of input, and what you would want the output to be from that.

    Caution: Contents may have been coded under pressure.
      Hi Roy,

      Thanks for the help. The params 0/1 ask for a company name and country. For privacy sake, I'll just give the first letter. Here is an example of what I get now:
      J... ( Korea ) L... ( Korea ) A... ( France ) G... ( USA ) I... ( France ) M... ( Brazil )

      This is what I would like:
      A... ( France ) G... ( USA ) I... ( France ) J... ( Korea ) L... ( Korea ) M... ( Brazil )

      Does this help? Thanks!
        I'm pretty sure that ysth's response is what you're looking for.

        Caution: Contents may have been coded under pressure.
Re: sort after split
by aquarium (Curate) on Dec 05, 2007 at 22:21 UTC
    you get the empty parens because the array is empty. why do you try to chomp the array? chomp $entry instead, as the first line of the loop.
    the hardest line to type correctly is: stty erase ^H
      Thanks Aquarium,

      I tried chomping then entry, but my list still prints in a sequential manner. Losing my mind :)
      open (DB1,"logfile.log") or die("There was a problem opening the file. +"); while ($entry=<DB1>) { chomp $entry; @fields = sort { $a cmp $b } split /\t/, $entry; #@fields=split(/ /,$entry); #chomp @fields; foreach $entry (@fields){ $_ =~ s/\n/\,/; }

      Any other ideas? I really appreicate your help all. Thank a Million.

      lT

        You seem to have missed (or deliberately ignored?) the question asked by Roy Johnson above. I'm sure that he won't object if I repeat his post in its entirety:

        What does your data look like? Give a few sample lines of input, and what you would want the output to be from that.