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

Re: [PBML] Sorting Help

Expand Messages
  • 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 1 of 4 , Aug 7, 2000
    • 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.