Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] Sorting Help

Expand Messages
  • Gregory D. Fox
    I got this code from another list and it worked nicely. ... @searchresults = sort by5thfield @searchresults; sub by5thfield{ (@crap[1..4], $a5thfield) = split
    Message 1 of 4 , Aug 7 2:38 PM
    • 0 Attachment
      I got this code from another list and it worked nicely.
       
      ---Code---
      @searchresults = sort by5thfield @searchresults;

      sub by5thfield{

      (@crap[1..4], $a5thfield) = split (/\|/, $a);
      (@crap[1..4], $b5thfield) = split (/\|/, $b);
      "\U$a5thfield\E" cmp "\U$b5thfield\E";
      #forcing them to upper case overides perl sorting upper and lower
      #case seperately
      }
       
      Thanks for the help

      Gregory D. Fox
      Greg@...
      <><<><<><<><<><<><<><<><<><<><
      Need an eCommerce Solution?
      Visit:
      http://eCommerceCart.com
      <><<><<><<><<><<><<><<><<><<><
      We also provide web site design and
      hosting. Visit us online at:
      http://LibertyMarketing.com
      <><<><<><<><<><<><<><<><<><<><

      -----Original Message-----
      From: Smith, Sheldon [mailto:sheldon.smith@...]
      Sent: Monday, August 07, 2000 5:32 PM
      To: perl-beginner@egroups.com
      Subject: RE: [PBML] Sorting Help

      Did you omit something? I see a nice, big split to filter the desired
      elements out of the inventory list. Once you add the entire inventory record
      to the searchresults list, where do you (re-split and) sort that?

      > -----Original Message-----
      > From: Gregory D. Fox [mailto:greg@...]
      > Sent: Sunday, August 06, 2000 1:14 PM

      > Hi All,
      >
      > I am trying to sort the following @searchresults array by
      > $current_itemname.
      > I have tried different combinations and still get the same results.
      >
      > The code is below. Any help is appreciated.
      >
      > TIA...
      >
      > ----- CODE ----
      > foreach (@inventory){
      >      ($current_code, $current_category, $current_price,
      > $current_shipping,
      > $current_itemname, $current_description, $current_keywords,
      > $current_inv_qty, $current_imagename, $current_colors, $current_sizes,
      > $current_comments, $current_taxable) = split (/\|/);
      >
      >      if ($category_search =~ /$current_category/i && $action
      > eq "search by
      > $catvar" ){
      >            if($current_category){
      >               push @searchresults, $_;
      >                 }
      >      }
      > }
      >
    • Smith, Sheldon
      I believe you can also use undef as a list target, that is, (undef,undef,undef,undef,$a5thfield) = split... and so on. Since Perl doesn t have to make a junk
      Message 2 of 4 , Aug 7 2:57 PM
      • 0 Attachment
        I believe you can also use "undef" as a list target, that is,
        (undef,undef,undef,undef,$a5thfield) = split... and so on. Since Perl
        doesn't have to make a junk list and then do the assignments, it should run
        (slightly) faster. This would really only help if you are in the by5thfield
        routine a lot, and/or you are running on a slower machine.
        --S

        -----Original Message-----
        From: Gregory D. Fox [mailto:greg@...]
        Sent: Monday, August 07, 2000 4:38 PM


        I got this code from another list and it worked nicely.


        ---Code---
        @searchresults = sort by5thfield @searchresults;

        sub by5thfield{

        (@crap[1..4], $a5thfield) = split (/\|/, $a);
        (@crap[1..4], $b5thfield) = split (/\|/, $b);

        "\U$a5thfield\E" cmp "\U$b5thfield\E";
        #forcing them to upper case overides perl sorting upper and lower
        #case seperately
        }

        Thanks for the help
      • Andrew Johnson
        ! I was reading along with this sorting thread and wished to try to ! resort a data file..... I used one of the examples posted..... but your question is not a
        Message 3 of 4 , Aug 7 9:46 PM
        • 0 Attachment
          ! I was reading along with this sorting thread and wished to try to
          ! resort a data file..... I used one of the examples posted..... but

          your question is not a sorting question.

          ! this does not work

          'does not work' is not a very useful description of a problem -- try
          to be a little more forthcoming in the future. On the other hand, the
          code you posted is seriously broken, so perhaps 'does not work' is
          the best analysis you could offer -- but that is the danger with
          just copying code that you don't really understand.

          ! ...... could someone have a look. What I am trying to do is take a
          ! file that use eight fields and re-order them.... well seven of them

          Here are some problems with your script:

          You do not check the return values on both of your open() statements,
          they should be:

          open (IN, $infile) || die "can't open $infile: $!";
          open (OUT, ">$outfile")|| die "can't open $outfile: $!";

          In your while(<>) statement you do not specify the IN filehandle to
          read from ... it should be:

          while(<IN>){

          You probably also want to chomp() off the newline from each line
          of data (you don't want a newline in your 8th field right?)

          In your split() line, you do not have a comma after the first
          argument to split() -- it should be:

          ... = split(/,/,$_)

          In your print() line, you are printing to STDOUT -- if you wanted to
          print to the OUT filehandle you'll need to specify it on the print()
          line:

          print OUT "blah blah blah\n";

          However, you can simplify your script considerably by splitting into
          an array and then using an array slice to print out the reordered
          fields:

          #!/usr/bin/perl -w
          use strict;

          die "incorrect number of arguments\n" unless @ARGV == 2;

          my $infile = shift;
          my $outfile = shift;

          open (IN, $infile) || die "can't open $infile: $!";
          open (OUT, ">$outfile")|| die "can't open $outfile: $!";

          while (<IN>) {
          chomp;
          my @fields = split /,/;
          print OUT "@fields[0,7,2,3,4,5,6]\n";
          }
          close (IN);
          close (OUT);

          __END__

          But the above is only a single fish and won't keep you fed long --
          and it may not even be the right fish for you (if your data has
          comma's within one or more of the fields for example, or a record
          does not have the expected number of fields, etc ...).

          You'll need to investigate the documentation a good deal further if
          you hope to keep eating. Based on the problems with your script, I'd
          recommend:

          perldoc perl
          perldoc perldata
          perldoc perlsyn
          perldoc perlopentut

          for starters, and supplement your diet with healthy doses of the
          perlfaqs as well (perldoc perlfaq).

          regards,
          andrew

          --
          Andrew L. Johnson http://members.home.net/perl-epwp/
          They're not soaking, they're rusting!
          -- my wife on my dishwashing habits
        Your message has been successfully submitted and would be delivered to recipients shortly.