I suppose it does look a little bizarre.  Actually, my goal is a little
more complex.  We have a simulation that outputs data files, but often up
to 90% of the data is redundant.  So I'm trying to write a filter for the
data.  I have to:

1.  open and load the file
2.  strip all comments (marked with a #) and blank lines off the top
3.  sort by column 4, then by column 3
4.  remove all lines at the top that have a 0 in column 5
5.  write the comments + sorted lines back out to a new file

Unfortunately I'm new enough at perl that I've only got steps 1 and 2 to
work so far...  I got some great help here on the list for step 3, though
the code at
<http://xarch.tu-graz.ac.at/publ/tutorial/perl/FMTEYEWTK/sort.html> seems
much more concise.  I just haven't gotten it to work on my array instead of
a string.

- B

__________________


On Apr 8, Bryan R Harris said:

>$file = "somefile.dat";
>open (FILE, $file)  || die("Couldn't open $file: $!\n");
>@_ = <FILE>;
>close(FILE);
>while ($_[1] =~ /^[#\n]/) { push(@comments, shift(@_)); }
>print "@comments";

This is a rather bizarre way to do this task, by the way.  It also fails
in some cases -- if the FIRST line of the file starts with a # or is
blank, it gets pushed to @comments, even though you're trying to prevent
that.

Here's how I would write the code:

  open FILE, "< $file" or die "can't read $file: $!";
  while (<FILE>) {
    print unless /^[#\n]/;
  }
  close FILE;

--
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to