LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   perl string match issue (https://www.linuxquestions.org/questions/linux-newbie-8/perl-string-match-issue-695369/)

knockout_artist 01-06-2009 11:13 AM

perl string match issue
 
Hi every one,


Code:

#!/usr/bin/perl

open FILE, "<", "test-last" or die $!;


while (<FILE>) {

$test= $_;  //current line become $test

@names=qw( one two three four );    //aray to hold few values

foreach  $names(@names){
print " $test";  print "$names";  // now here it matches!! i mean string from file and from //aray

if($test eq $names){

print "match found \n";  //doenst print a thing

}

test-last
Code:

five
six
two
seven



See "two" should print "match found" but it doesn't.

Any ideas?

Thanks

angrek 01-06-2009 12:06 PM

You left out two ending brackets when you pasted your code btw. :)

If you put a

Code:

print "Length of test is " . length($test) . "\n";
in your code you'll notice that it's one character longer than the actual length of your $test variable that you're pulling from the file. You're pulling a newline character. After your foreach line add a

Code:

chomp($test);
and it should work fine.

mk27 01-06-2009 12:13 PM

That's because the lines from the file have a newline at the end, whereas the ones in your array do not. Use chomp to remove it:
Code:

#!/usr/bin/perl -w
use strict;

my @names=qw( one two three four );    # no need to redeclare this each time in the loop!

while (<DATA>) {
        my $test=$_;
        chomp $test;        # removes newline
        foreach  $name(@names){
                print "\"$test\" \"$name\"\n"; # using escaped quotes you could have seen this 
                if($test eq $name) {print "MATCH FOUND!\n"}
        }
}

__DATA__
five
six
two

You can read in data from the end of a script this way the same as if it were in a file. Using the escaped quotes in your original version you would have seen this:

"five
" "one"
"five
" "two"


"perlmonks" is a good and very active forum for perl help.


ps. why did you use c-style comments?

knockout_artist 01-06-2009 12:32 PM

Quote:

Originally Posted by mk27 (Post 3399033)


ps. why did you use c-style comments?

I am deeply in love with C :(, . the first language I learned second would be perl.
I love playing on hardware level with C. Its awesome

knockout_artist 01-06-2009 01:50 PM

Thank you very much both of you,
mk27 thanks for pointing out loop thingie.

knockout_artist 01-06-2009 02:03 PM

Good time to ask an old question

Code:

my @names=qw( one two three four );    # no need to redeclare this each time in the loop!

while (<DATA>) {
        my $test=$_;

why did you use "my" before declaring array and variable ?

Thanks

angrek 01-06-2009 02:11 PM

Quote:

Originally Posted by knockout_artist (Post 3399121)
Thank you very much both of you,
mk27 thanks for pointing out loop thingie.

Yeah, sorry about that... didn't mean to put it in the loop there.

chrism01 01-06-2009 06:43 PM

@knockout-artist; ALL perl programs should start with

Code:

#!/usr/bin/perl -w
use strict;

which will warn you about syntax errors and dodgy code usage. Never code without them :)
An alternative to '-w' is 'use warnings;'

Also, if you want to do a test compile without running the prog, use

perl -wc prog.pl

If you 'use strict;' then you have to declare your vars using 'my', which restricts their scope to the current block (usually current subroutine). IOW, avoid globals as much as possible.
Think C (also my main lang prev to Perl).
:)

Good Perl links:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials

knockout_artist 01-13-2009 11:45 AM

Code:


#!/usr/bin/perl
use strict;

my $flag =0 ;      #used to flags
my $flag2 =0;
my @names=qw(  ); #empty array
my $i=0;          # mostly for debugging
my $j=0;          #same as above
my $test;          #hold value from __DATA__

while(<DATA>)    #read all 4 valuse from  DATA
{
print "$flag Flag at start\n";  #debugging
my $test=$_;
chomp($test);

if ($j==0 ){                  #add first value in the array so in next loop it would have some thing to look at
print "push $test into names array\n";
  push (@names, "$test" );
  }
print "name from DATA $test\n";    #debug  print value read from data

foreach my $names(@names){          #inner loop  for @names array
print " Test==$test Names==$names \n";    #debug
if($test eq $names){                      #make sure that if it already exist
$flag = 2;                                  #playing with those to flags

$flag2 =1;

}else{

$flag =0 ;
}
if ($flag != 2 ){

$flag = 0;

}

}
print " test = $test  flag = $flag  \n";
$j++;
if($flag ==0){
push (@names, "$test" );      #Tried differnt flag

}
print "flag2 $flag2\n";
}
$flag2 =0;
print "@names\n";

__DATA__
one
one
three
one



What I need is out put
[qoute]one three[/quote]

what I get is

Quote:

one three one

output ::

Code:

  ./last-log2.pl
0 Flag at start
push one into names array
name from DATA one
 Test==one Names==one
 test = one  flag = 2 
flag2 1
2 Flag at start
name from DATA one
 Test==one Names==one
 test = one  flag = 2 
flag2 1
2 Flag at start
name from DATA three
 Test==three Names==one
 test = three  flag = 0 
flag2 1
0 Flag at start
name from DATA one
 Test==one Names==one
 Test==one Names==three
 test = one  flag = 0 
flag2 1
one three one  ## print whats inside the array



Any ideas?
Thanks!

knockout_artist 01-13-2009 02:55 PM

BTW

I was able to resolve it.
I will post the code tomorrow.

Thank you every one. :-)

chrism01 01-13-2009 05:48 PM

Don't forget the -w switch (or 'use warnings').

I'd also change

while(<DATA>)

to

while( defined($test = <DATA>) )

removing need for

my $test=$_;


Try indenting your code as well, its easier to read...

GaijinPunch 01-13-2009 05:53 PM

Seconded on warnings & strict.
My Perl places will tell you to put those in and run your code again before bothering to help... although I don't think they would've helped here.


All times are GMT -5. The time now is 09:25 AM.