Re: Comments on parsing solution.

From: Tore Aursand (tore_at_aursand.no)
Date: 11/20/03


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


Relevant Pages

  • Re: return the array index for a given value
    ... > is there a ready perl function to return the index of a given value in ... > an array? ... "A teacher is never a giver of truth - he is a guide, a pointer to the truth that each student must find for himself. ...
    (comp.lang.perl.misc)
  • Re: Any Falsely Accused Sex Offenders Wish to Share Their Story?
    ... ronniecat wrote: ... truth, the whole truth and nothing but the truth but instead wrote: ... |>|student teacher - and despised it so much I switched out the B.Ed ... |>your partial quote leaves a different impression. ...
    (alt.true-crime)
  • Re: Jiddu Krishnamurti y Who Brings the Truth?.
    ... Who Brings the Truth? ... Teacher meant to me and what the Truth was behind the form of the ... Krishnamurti, was the vehicle of the World-Teacher because many people ...
    (talk.religion.buddhism)
  • Re: Jiddu Krishnamurti y Who Brings the Truth?.
    ... Who Brings the Truth? ... Teacher meant to me and what the Truth was behind the form of the ... Krishnamurti, was the vehicle of the World-Teacher because many people ...
    (talk.religion.buddhism)
  • Jiddu Krishnamurti y Who Brings the Truth?.
    ... Jiddu Krishnamurti y Who Brings the Truth?. ... Teacher meant to me and what the Truth was behind the form of the ... was the vehicle of the World-Teacher because many people ...
    (soc.culture.vietnamese)