LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-06-2009, 12:13 PM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
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
 
Old 01-06-2009, 01:06 PM   #2
angrek
LQ Newbie
 
Registered: Jan 2009
Posts: 7

Rep: Reputation: 2
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.
 
Old 01-06-2009, 01:13 PM   #3
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
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?
 
Old 01-06-2009, 01:32 PM   #4
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by mk27 View Post


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
 
Old 01-06-2009, 02:50 PM   #5
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
Thank you very much both of you,
mk27 thanks for pointing out loop thingie.
 
Old 01-06-2009, 03:03 PM   #6
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
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
 
Old 01-06-2009, 03:11 PM   #7
angrek
LQ Newbie
 
Registered: Jan 2009
Posts: 7

Rep: Reputation: 2
Quote:
Originally Posted by knockout_artist View Post
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.
 
Old 01-06-2009, 07:43 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
@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
 
Old 01-13-2009, 12:45 PM   #9
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
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!
 
Old 01-13-2009, 03:55 PM   #10
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
BTW

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

Thank you every one. :-)
 
Old 01-13-2009, 06:48 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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...
 
Old 01-13-2009, 06:53 PM   #12
GaijinPunch
Member
 
Registered: Aug 2003
Location: Tokyo, Japan
Distribution: Gentoo
Posts: 130

Rep: Reputation: 22
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.
 
  


Reply

Tags
perl, regexp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep question - match exact string Panagiotis_IOA Linux - General 2 01-20-2014 05:34 AM
how to match for multiple pattern at the end of given string Santoshkb Programming 2 06-23-2008 11:42 AM
iptables string match htb Linux - Networking 2 08-30-2006 03:37 PM
iptables string match kahpeetan Linux - Security 3 11-09-2003 07:36 PM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 10:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration