Re: Comments on parsing solution.
From: Tore Aursand (tore_at_aursand.no)
Date: 11/20/03
- Next message: Richard S Beckett: "Win32-OLE equivalent of write_row"
- Previous message: Glenn Jackman: "Re: Comments on parsing solution."
- In reply to: Prabh: "Comments on parsing solution."
- Next in thread: Eric J. Roode: "Re: Comments on parsing solution."
- Reply: Eric J. Roode: "Re: Comments on parsing solution."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 20 Nov 2003 17:34:50 +0100
On Thu, 20 Nov 2003 06:00:09 -0800, Prabh wrote:
> #!/usr/local/bin/perl
You _need_ this:
use strict;
use warnings;
> open(FDL,"Foo.txt") ;
> chomp(@arr = <FDL> ) ;
> close(FDL) ;
Always check the return value of open():
open( FDL, 'Foo.txt' ) or die "$!\n";
> undef @files ;
> foreach $line ( @arr ) {
> push(@files,(split(/\:/,$line))[0]) ;
> }
Why do you want to set @files to undefined? This should do it, and it
keeps @files unique too;
my @files = ();
my %seen = ();
foreach ( @arr ) {
my $file = ( split(/\:/) )[0];
push( @files, $file ) unless ( exists $seen{$file} );
}
> foreach $file ( &uniq(sort @files ) ) {
> undef $info ;
> $info = grep {/^$file\:/} @arr ;
> printf "$info lines of info on $file\n";
> }
And this could be written as (no need for 'printf'):
foreach ( sort @files ) {
my $info = grep { /^$file\:/ } @arr;
print "$info lines of into on $file\n";
}
> sub uniq {
AFAIKT, this won't work if you give it an array of files where two
identical filename doesn't follow each other;
perldoc -q duplicate
You don't need this function, though, as my code (above) keeps the array
unique at the point it's being populated.
-- Tore Aursand <tore@aursand.no> "A teacher is never a giver of truth - he is a guide, a pointer to the truth that each student must find for himself. A good teacher is merely a catalyst." -- Bruce Lee
- Next message: Richard S Beckett: "Win32-OLE equivalent of write_row"
- Previous message: Glenn Jackman: "Re: Comments on parsing solution."
- In reply to: Prabh: "Comments on parsing solution."
- Next in thread: Eric J. Roode: "Re: Comments on parsing solution."
- Reply: Eric J. Roode: "Re: Comments on parsing solution."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|