Perl chomp function not working properly

732 Views Asked by At

i have a file which contains something like this

name: A id: B cl: C
name: D id: E cl: F
name: I id: G cl: K

i am grep the contents after cl: and storing it in an array

            #!/usr/bin/perl
            @success=qw(C J K L);
            @arr=qw(P M C);
            my $pattern="is present here";
            local $/;
            open (<FILE>, sl.txt);
            my $ln=<FILE>;

            while($ln=<FILE>)
            {

             if($ln=~$pattern)
             {

             {local $/="\n"; @using=`grep "cl" sl.txt | cut -d " " -f 6 `;}
                         print "\n the using array is  @using \n";
                         print "$using[0] ,$using[1] \n";
                         chomp(@using);
                         print" after chomp @using\n";
                foreach my $lb (@using)
                {
                 if($lb eq $success[2])
                 {
                  print " comparison true\n";
                 }         
                 else
                 {
                  print " false comparison\n";
                 }
                }
              }
             }
             close(<FILE>);

please check why this comaprison is failing after grepping and chomp . the before chomp @using and after chomp @using is the same

1

There are 1 best solutions below

2
On

Given this script:

#!/usr/bin/env perl
use strict;
use warnings;

my @using = qx{grep "cl:" sl.txt | cut -d " " -f 6};
print "Before:\n";
print "[$_]\n" foreach (@using);
chomp @using;
print "After:\n";
print "[$_]\n" foreach (@using);

And this data file sl.txt:

name: A id: B cl: C
name: D id: E cl: F
name: I id: G cl: K

Perl 5.18.1 on Mac OS X 10.9.1 yields:

Before:
[C
]
[F
]
[K
]
After:
[C]
[F]
[K]

This looks like the code behaves correctly. What do you get from each of your scripts when you put the debugging print loops in place? (Using qx{ … } is another way of writing back quotes.)