in reply to Re: parsing long text file
in thread parsing long text file

Sorry I am not running it from the command line the <> operator is there but it can be replaced I am looking for a better way to parse the file; here is what I have on top of the code I send. It have to display to the browser.

my $start = $getdata;
my $end = $getdata2;

my $dir = '../weblog/';
@ARGV = ();

die "start must be less than end" if $start >= $end;
die "no dir $dir here" unless -d $dir;

find sub {
my $numb = (fileparse($_,'.txt'))[0];
return unless $numb =~ /^\d+$/;
push @ARGV, $File::Find::name if $numb >= $start and $numb <= $end; }, $dir;
die "no .txt files found in $dir" unless @ARGV;

Replies are listed 'Best First'.
Re: Re: Re: parsing long text file
by talexb (Chancellor) on Jun 18, 2002 at 15:27 UTC
    Please put CODE tags around your code so that we can read it .. that means <CODE> at the beginning and </CODE> at the end. Also, see if you can follow some kind of regular indentation: the closing brace for your subroutine (if that's what find sub is) is hidden near the end of the second line.

    Please post code that compiles cleanly and looks a little better than that, then we can check it out.

    --t. alex

    "Mud, mud, glorious mud. Nothing quite like it for cooling the blood!" --Michael Flanders and Donald Swann

      Sorry, here is the code again, thanks for your replay!
      my $start = $getdata; my $end = $getdata2; my $dir = '../weblog/'; @ARGV = (); die "start must be less than end" if $start >= $end; die "no dir $dir here" unless -d $dir; find sub { my $numb = (fileparse($_,'.txt'))[0]; return unless $numb =~ /^\d+$/; push @ARGV, $File::Find::name if $numb >= $start and $numb <= $end; }, $dir; die "no .txt files found in $dir" unless @ARGV; while ( <> ) { $p1="P1 = (inquiry, launch page)\n"; $p2="P2 = (car coverages, endorsements, operators)\n"; $p3="P3 = (notepad, scratch)\n"; $b1="B1 = (my inquiry: auto, home and location)"; $p0="P0 = (local search)\n"; $c0="C0 = (dist search)\n"; $c1="C1 = (state inquiry)\n"; $c3="C3 = (test notepad)\n"; $h1="H1 = (owners and coop search and history)\n"; if (m/iapw_p1/g){s/iapwp1/$p1/g; print;} if (m/iapw_p2/g){s/iapwp2/$p2/g; print;} if (m/iapw_p3/g){s/iapwp3/$p3/g; print;} if (m/iapw_b1/g){s/iapwb1/$b1/g; print;} if (m/iapw_p0/g){s/iapwp0/$p0/g; print;} if (m/iapw_c0/g){s/iapwc0/$c0/g; print;} if (m/iapw_c1/g){s/iapwc1/$c1/g; print;} if (m/iapw_c2/g){s/iapwc3/$c3/g; print;} if (m/iapw_c3/g){s/iapwh1/$h1/g; print;} }
        Just looking at the rule checking, I can recommend putting the rules into a hash:
        #!/usr/bin/perl -w use strict; # Define substitution rules using a hash. Note that these rules are # stored in no particular order .. if this is very slow, efficiences # can be achieved by changing to an array of hashes, with the most # likely rule checked first. my %Rules = ( "p1" => "P1 = (inquiry, launch page)\n", "p2" => "P2 = (car coverages, endorsements, operators)\n", "p3" => "P3 = (notepad, scratch)\n", "b1" => "B1 = (my inquiry: auto, home and location)", "p0" => "P0 = (local search)\n", "c0" => "C0 = (dist search)\n", "c1" => "C1 = (state inquiry)\n", "c3" => "C3 = (test notepad)\n", "h1" => "H1 = (owners and coop search and history)\n" ); # Process the input stream. while ( <> ) { foreach ( keys %Rules ) { if ( m/iapw_$_/ ) { s/iapw_$_/$Rules{ $_ }/ge; print; # Is there only one possible subsitution per line? If so, # we could use a 'last' statement here to go on to the next # line. } } }

        Your logic for opening up the log files is bizarre and does not compile on my system, but I'll presume that you have that part of the operation working correctly.

        I can strongly recommend 'Learning Perl' from O'Reilly books. Read through that, try as many examples as you can, and you'll be far better off to tackle Perl projects.

        --t. alex

        "Mud, mud, glorious mud. Nothing quite like it for cooling the blood!" --Michael Flanders and Donald Swann