Merg_1
Merg_1

Reputation: 11

Perl Function Not Running

My Perl code:

use strict;
use warnings;
 
my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";
 
while (my $row = <$fh>) {                
  chomp $row;
  @fields = split(/:/, $row);
if($fields[7] eq "?" || $fields[7] eq NULL)
  print "$row\n";
}


while (my $row = <$fh>) {             
  chomp $row;
  @fields = split(/:/, $row);
print '$fields: ' , replace($fields[1],"",$fields[1]) , "\n";}
}


while (my $row = <$fh>) {                
  chomp $row;
  @fields = split(/:/, $row);
if (index($fields[2], "-") != -1) {        
  print "$row\n";
}

while (my $row = <$fh>) {                
  chomp $row;
  @fields = split(/:/, $row);
if($fields[7] eq "Voyager2")        
  print "$row\n";
}

Error Message:

"my" variable $row masks earlier declaration in same statement at main.pl line 24.
"my" variable $row masks earlier declaration in same statement at main.pl line 25.
Global symbol "@fields" requires explicit package name (did you forget to declare "my @fields"?) at main.pl line 10.
Global symbol "@fields" requires explicit package name (did you forget to declare "my @fields"?) at main.pl line 11.
Global symbol "@fields" requires explicit package name (did you forget to declare "my @fields"?) at main.pl line 11.
syntax error at main.pl line 12, near ")
  print"
Global symbol "@fields" requires explicit package name (did you forget to declare "my @fields"?) at main.pl line 18.
Global symbol "@fields" requires explicit package name (did you forget to declare "my @fields"?) at main.pl line 19.
Global symbol "@fields" requires explicit package name (did you forget to declare "my @fields"?) at main.pl line 19.
Unmatched right curly bracket at main.pl line 20, at end of line
syntax error at main.pl line 20, near "}"
Can't redeclare "my" in "my" at main.pl line 30, near "(my"
main.pl has too many errors.

Not sure where I went wrong and how to fix errors

Upvotes: 0

Views: 181

Answers (1)

TLP
TLP

Reputation: 67900

You've got some messed up indentation there that is masking your issues. Lets fix that indentation and highlight the problem. Skipping the error free parts.

while (my $row = <$fh>) {
    chomp $row;
    @fields = split(/:/, $row);                   # @fields undeclared
    if($fields[7] eq "?" || $fields[7] eq NULL)   # missing curly braces { } and bareword NULL needs quotes
    print "$row\n";
}

while (my $row = <$fh>) {
    chomp $row;
    @fields = split(/:/, $row);
    print '$fields: ' , replace($fields[1],"",$fields[1]) , "\n";
}     # EXTRA curly brace here
}

while (my $row = <$fh>) {
    chomp $row;
    @fields = split(/:/, $row);
    if (index($fields[2], "-") != -1) {
        print "$row\n";
    }
# MISSING curly brace here

    while (my $row = <$fh>) {   # this loop is now part of the previous one, and the 
        chomp $row;             # duplicate variable errors start
        @fields = split(/:/, $row);
        if($fields[7] eq "Voyager2") print "$row\n";  # MISSING curly braces
}

So what happens if we fix all these things? Well, we get this, which produces no errors.

my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
    or die "Could not open file '$filename' $!";
 
while (my $row = <$fh>) {
    chomp $row;
    my @fields = split(/:/, $row);
    if($fields[7] eq "?" || $fields[7] eq "NULL") {
        print "$row\n";
    }
}

while (my $row = <$fh>) {
    chomp $row;
    my @fields = split(/:/, $row);
    print '$fields: ' , replace($fields[1],"",$fields[1]) , "\n";
}

while (my $row = <$fh>) {
    chomp $row;
    my @fields = split(/:/, $row);
    if (index($fields[2], "-") != -1) {
        print "$row\n";
    }
}

while (my $row = <$fh>) {
    chomp $row;
    my @fields = split(/:/, $row);
    if($fields[7] eq "Voyager2") {
        print "$row\n";
    }
}

But what is it that we have? This code will not work as expected. You have duplicated 4 while loops, and the first loop will exhaust the file handle, leaving the other loops without lines to process. Meaning they will never be executed. Unfortunately, since it is hard to say what you were trying to do here, it's hard to say how to fix it.

My guess is that you thought you could read lines in order with a while loop. Perhaps last is what you are looking for, to skip out of the while loop when you find a condition. Perhaps while is wrong for you, and you meant to take line 1, then line 2, etc. Then you need to remove all the while loops.

Without more information, the TL;DR is: Your code does not work.

Upvotes: 4

Related Questions