Skyler99 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I'm trying to troubleshoot this code that is suppose to supress a line once it finds a string within a string. I would use regex pattern but I prefer to use INDEX unless if there is a perfect way to use it in the code. I tried to read the file with the script but it keeps failing with "Use of uninitialized value" I'll appreicate if you could let me knowhwat I need to chnagei the code to make it work. Thanks in advance.
#!perl -w use strict; my $buffer=""; my $suppress = 0; my @field; my %newline; # my %partofblock; my %skipaccount; $skipaccount{$_} = 1 for qw(77403 77404 77406 77407 77408 77409 77411 +77412 77413 77414 77416 77418); my $infile = 'c:/rx_q.008'; my $outfile = 'c:/ftpscript/idxfile.txt'; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; $newline{$_} = 1 for qw(MSH); while (<IN>) { chomp(); if ($newline{$field[0]}) { processbuffer(); } if ( index($newline{$_}, $skipaccount{$_}) != -1) { $suppress = 1 if $skipaccount{$field[7]}; } $buffer .= "$_\n"; } processbuffer(); sub processbuffer { print OUT $buffer unless $suppress; $buffer=""; $suppress = 0; }

Replies are listed 'Best First'.
Re: Supress a line every time it finds a string
by submersible_toaster (Chaplain) on Apr 08, 2003 at 06:38 UTC

    Improv makes some valid points, I'll render them down to - 'This code is difficult to interpret' , by humans and perl alike it would seem.

    So lets, get forensic on it. You initialising some funky stuff here, some of it looks like you plan to add to it later.

    #!perl -w use strict; my $buffer=""; my $suppress = 0; my @field; my %newline; $newline{$_} = 1 for qw(MSH); my %skipaccount; $skipaccount{$_} = 1 for qw(77403 77404 77406 77407 77408 77409 77411 +77412 77413 77414 77416 77418); my $infile = 'c:/rx_q.008'; my $outfile = 'c:/ftpscript/idxfile.txt'; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; while (<IN>) {
    # Ok now $_ has scalar input from <IN>
    chomp();
    # and it's newline chomped # here is where it is confusing, there is nothing in @field, # let alone at $field[0] , do you mean to split up $_ into @field ???
    if ($newline{$field[0]}) { processbuffer(); }
    # OK , I am certain this is not doing what you expect #if ( index($newline{$_}, $skipaccount{$_}) != -1) { # $suppress = 1 if $skipaccount{$field[7]}; #} # What you're aiming at (IMHO) is to set suppress to 1 if # the item at index 7 in your fields array (which is split from $_ ri +ght??) # is a member of the skipaccount hash. # the if (index($newline{$_} ) makes no sense to me, not in the least + with # the data that appears to be in those objects.
    $buffer .= "$_\n"; } # Personally I prefer calling functions like this with an arg, rather +than # relying on a globar ($buffer in this case). processbuffer(); sub processbuffer { print OUT $buffer unless $suppress; $buffer=""; $suppress = 0; }

    I can't believe it's not psellchecked
Re: Supress a line every time it finds a string
by Improv (Pilgrim) on Apr 08, 2003 at 05:05 UTC
    Hello, I wasn't able to understand what you're trying to do exactly from your description, but I did look at your code, and I'm puzzled by it. A few oddities:
    • Where do you assign into @field?
    • Why do you use the for syntax in $newline{$_} = 1 for qw(MSH);?
    • Your program flow might be overly complex -- I have a pretty strong intuition that, if I understood what you're trying to do, that it would be a much simpler block of code.
    So, if you can elaborate exactly what you're trying to do, I'm sure we'll be able to help you.
Re: Supress a line every time it finds a string
by CountZero (Bishop) on Apr 08, 2003 at 05:42 UTC

    There is definitely something wrong with your code and the use of @field. It never gets assigned to and hence cannot be used as a meaningful key into your hashes %newline or %skipaccount. Is perhaps some part of your code missing?

    Also it would help if you could give us an example of the content of the rx_q.008 file so we can see what you are dealing with.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law