-2

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

user3095218
  • 39
  • 1
  • 9
  • Do you really get `C D` and not `C F`? – Jonathan Leffler Jan 28 '14 at 06:26
  • 1
    If I'm understanding what you're saying, `$using[0]` contains `"C\nD"`. chomp removes newlines from the ends of strings, not from the middle of them. – DavidO Jan 28 '14 at 06:27
  • what does `use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper \@using;` show just after the `@using=` statement? – ysth Jan 28 '14 at 06:31
  • did sl.txt originate on a windows system? – ysth Jan 28 '14 at 06:31
  • ya sorry its printing C F. yes david you are right its priting "C\nD" but in my first script its coming correct . since chomp @using will remove newline characters i had used it . any suggestions please help – user3095218 Jan 28 '14 at 06:47
  • no sl.txt is from linux system only. – user3095218 Jan 28 '14 at 06:50
  • 2
    Are we allowed to suggest that Perl can read files and split on blanks very effectively without needing to use external programs? You need to show us what is different between the two scripts, because there must be a difference for them to behave differently. In fact, you should really show us two Perl scripts, one showing the problem and one not showing the problem. They should be minimal, of course. But in practice, there's going to be a critical difference. – Jonathan Leffler Jan 28 '14 at 06:56
  • The difference is probably in what the unix commands are outputting. You should examine the array prior to doing the chomp() and look for differences between the output of the two scripts. – Scooter Jan 28 '14 at 07:09
  • re "any suggestions", see my comment above ("what does...") – ysth Jan 28 '14 at 07:28
  • hi ysth sorry i missed your comment above ...i am new to perl . the output it is printing is " $VAR1 = [ "C\n,F\n,K\n" ]; and in the failure script it is printing $VAR1 = [ "C\nF\nK\n" ]; – user3095218 Jan 28 '14 at 10:32
  • @ysth the output OF successful script is printing is " $VAR1 = [ "C\n,F\n,K\n" ]; and in the failure script it is printing $VAR1 = [ "C\nF\nK\n" ]; – user3095218 Jan 28 '14 at 10:52
  • The line of code ``@using=`grep "Lab:" /path/sl.txt | cut -d " " -f 6 `;`` from your example means a different data file from the one you show in the question (`Lab` vs `cl`). This presents problems. Most of the code after this line is irrelevant to reproducing the problem, apart from a few close braces; all the conditional code (two `if` blocks) before it is irrelevant to the problem. Reduce your test case, and show 3 lines of the actual data file — or revise the code to work with the original data file. Don't place files in absolute locations; most people won't have a directory `/path`. – Jonathan Leffler Jan 28 '14 at 14:23
  • what versions of perl are you using on the two systems (perl -v will tell you) – ysth Jan 28 '14 at 17:19
  • what is `$/` set to? it appears you are setting it, so the backticks don't separate on newlines (though you don't show that in the script). or you are not showing the output correctly? is it really `["C\n","F\n","C\n"]`? – ysth Jan 28 '14 at 17:21
  • @ysth both the scripts are running on v5.8.7 and i have just given "local $/ "in my script before opening the file . then i am trying to grep it . i got to know that "local $/" will move the entire contents of a file to a single variable ... will that be affecting . but i want the contents of a file in a variable also. then later i am using while(my $ln=) and inside that i am using if(true){ do this} else{ grep file and chomp } – user3095218 Jan 29 '14 at 05:08
  • it looks to me like you want lines from cut to be in separate elements; for that you have to not have `$/` be undef (which it is after your local.) – ysth Jan 29 '14 at 05:37
  • yes you are right i want the cut to be in separate elements so @ysth is there any way to disable $/ after reading it into variable . but acually i am giving local $/ ; open while(my $ln=) and inside that i am using if($ln~="pattern"){ do this} else{ grep file and chomp } so will it still effect the other file which i am trying to grep. how can i achieve this solution – user3095218 Jan 29 '14 at 05:48
  • you can change it just where the grep is like: `{ local $/="\n"; @using = `...`; }` – ysth Jan 29 '14 at 08:04
  • @ysth after using the {local $/=....;} i am getting output as $VAR1 = [ "C\n", "F\n" ]; shown for 2 (C and F) . i am displaying 0th element and 1st element "before and after chomp before C before F after C after F" and finally displaying using array it is showing as "C\nF" – user3095218 Jan 29 '14 at 09:23
  • @ysth there is a new line between before C before F and after C after F(it should have been continuously in two lines one below the other rather than space in between) – user3095218 Jan 29 '14 at 09:34
  • I'm not sure if you are saying there is still a problem or not. – ysth Jan 29 '14 at 09:48
  • there is still a problem ... as i told in previous comment . there is a new line between "before C before F and after C after F"(it should have been continuously in two lines one below the other rather than space in between) – user3095218 Jan 29 '14 at 10:03
  • @ysth please run the above edited script and see my output. you ll understand my above explanation of before chomp and after chomp and even though K eq K its not printing "comparison true" .. – user3095218 Jan 29 '14 at 14:35
  • @ysth thanks for telling me that the error was due to $/ . thank you :) – user3095218 Jan 30 '14 at 11:06

1 Answers1

1

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.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • That is ***NOT*** a minimal reproduction of the problem. Your also not using the data file you quoted — you're looking for `Lab:` in the code instead of `cl:`. Please get with the game — learn what it means to create an MCVE [How to create a Minimal, Complete, Valid Example?](http://stackoverflow.com/help/mcve). We'll help you; you have to enable us to help you — by providing a small program that reproduces the core problem, not 110 lines of … stuff like you've added to the question. Things like `/path/sl.txt` don't help, either. Use a simple `sl.txt` path name, for pity's sake! – Jonathan Leffler Jan 28 '14 at 07:54
  • hi . sorry i am new to perl. so didnt get u . but i am trying to grep after"cl" only . the faulty script gives output as this Before: "[C F ]" After: "[C F ]" .."i have only shown u C and F". i am grepping from the same file sl.txt. but from correct file it is "Before: [C ] [F ] [K ] After: [C] [F] [K]" as shown by you – user3095218 Jan 28 '14 at 09:05