Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/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.
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
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.
@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).
#!/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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.