Search code examples
perlcygwin

Why I am not getting "success" with this program?


I have written the following program with the hope of getting success. But I could never get it.

my $fileName = 'myfile.txt';
print $fileName,"\n";

if (open MYFILE, "<", $fileName) {
    my $Data;
    {
        local $/ = undef;
        $Data = <MYFILE>;
    }
    my @values = split('\n', $Data);
    chomp(@values);

    if($values[2] eq '9999999999')  {
        print "Success"."\n";
    }

}

The content of myfile.txt is

160002
something
9999999999
700021

Solution

  • Try splitting by \s*[\r\n]+

    my $fileName = 'myfile.txt';
    print $fileName,"\n";
    
    if (open MYFILE, "<", $fileName) {
        my $Data;
        {
            local $/ = undef;
            $Data = <MYFILE>;
        }
        my @values = split(/\s*[\r\n]+/, $Data);
    
        if($values[2] eq '9999999999')  {
            print "Success";
        }
    
    }