CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
why two same valued variables not equal ?

 



robinson
Novice

Feb 3, 2009, 1:54 PM

Post #1 of 18 (2144 views)
why two same valued variables not equal ? Can't Post

Hello!

I created two variable with same value, one was assigned using "=", the other was read from a file, both have the same string ( I have checked the many times), such as "A009", but when I compare them using if ( $A eq $B), they are not equal. even when I compare $A or $B with the assigned string 'A009', they are not equal either! #@%

Below shows the code , the last comparison within for loop returned as not equal, what might be wrong?
Thanks in advance!


open ( IN_F0, "<$ARGV[0]") or die ("can not open !");
@W_list=<IN_F0>;
for ($i=0; $i<@W_list; $i++){
@temp=split/\s+/, $W_list[$i];
$Well_NO[$i]=$temp[1];
# print "$Well_NO[$i], $i\n\n";
}

$value = A009;
for($j=0; $j<$i; $j++)
{
print "$Well_NO[$j]\n";
#if( $Well_NO[$j] eq 'A009'){print "good !!!\n";}
if( $Well_NO[$j] eq $value ){print "good !!!\n";}
}
close(IN_F0);
~


eWish
User

Feb 3, 2009, 2:00 PM

Post #2 of 18 (2140 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

Well your code is not real pretty. Add a couple of things and see what happens.


Code
use strict; 
use warnings;


Declare you variables using 'my'

Code
my $var;


Print the values of each to the screen and see if the values are the same. Also, checkout http://perldoc.perl.org/functions/chomp.html to chomp the newline endings.


(This post was edited by eWish on Feb 3, 2009, 2:06 PM)


robinson
Novice

Feb 3, 2009, 2:28 PM

Post #3 of 18 (2135 views)
Re: [eWish] why two same valued variables not equal ? [In reply to] Can't Post

Thanks for the suggestion. but I have printed all the results and the 'A009' was indeed one of the string readed in, but the comprison just show not equal...

I have midified as you suggested, but the same result. To double check it, I assigned two other variable with different way using "=", and the comparion show not problem. faint...

below is the modified version:

#!/usr/bin/perl -w

use strict;
use warnings;

open ( IN_F0, "<$ARGV[0]") or die ("can not open !");

my @W_list=<IN_F0>;
my @Well_NO;
my $n;
for ( my $i=0; $i<@W_list; $i++){
my @temp=split/\s+/, $W_list[$i];
$Well_NO[$i] = $temp[1];
# print "$Well_NO[$i], $i\n\n";

$n=$i;
}
my $value='A009';
my $value1='A0'.'09';

print $value1;

print "\n\n";
if ($value eq 'A009')
{ print "good !!!\n" }

for(my $j=0; $j<$n; $j++)
{
print "$Well_NO[$j]\n";
if( $Well_NO[$j] eq 'A009'){print "good !!!\n";}
}

close(IN_F0);
~


FishMonger
Veteran / Moderator

Feb 3, 2009, 2:36 PM

Post #4 of 18 (2131 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

Please use the code tags when posting code.

Without seeing the contents of your file, it's hard if not impossible to say why your script is not working as you expect.


robinson
Novice

Feb 3, 2009, 2:52 PM

Post #5 of 18 (2125 views)
Re: [FishMonger] why two same valued variables not equal ? [In reply to] Can't Post

Thanks, I have also attacehd the original code and input file, both very small...

#!/usr/bin/perl -w

use strict;
use warnings;

open ( IN_F0, "<$ARGV[0]") or die ("can not open !");

my @W_list=<IN_F0>;
my @Well_NO;
my $n;
for ( my $i=0; $i<@W_list; $i++){
my @temp=split/\s+/, $W_list[$i];
$Well_NO[$i] = $temp[1];
print "$Well_NO[$i], $i\n\n";

$n=$i;
}
my $value='A009';
my $value1='A0'.'09';
print $value1;

print "\n\n";
if ($value eq $value1 )
{ print "good !!!\n" }

for(my $j=0; $j<$n; $j++)
Attachments: input.txt (0.27 KB)
  well.pl (0.54 KB)


KevinR
Veteran


Feb 3, 2009, 3:03 PM

Post #6 of 18 (2120 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

you need to chomp() the input as Fish already mentioned.
-------------------------------------------------


KevinR
Veteran


Feb 3, 2009, 3:05 PM

Post #7 of 18 (2118 views)
Re: [FishMonger] why two same valued variables not equal ? [In reply to] Can't Post

hes got it posted on devshed now too
-------------------------------------------------


robinson
Novice

Feb 3, 2009, 3:09 PM

Post #8 of 18 (2117 views)
Re: [KevinR] why two same valued variables not equal ? [In reply to] Can't Post

first time post, need some help urgently...


FishMonger
Veteran / Moderator

Feb 3, 2009, 3:23 PM

Post #9 of 18 (2114 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

http://perldoc.perl.org/functions/chomp.html


(This post was edited by FishMonger on Feb 3, 2009, 3:23 PM)


robinson
Novice

Feb 3, 2009, 3:49 PM

Post #10 of 18 (2110 views)
Re: [FishMonger] why two same valued variables not equal ? [In reply to] Can't Post

Thank you very much!

But after I applied chomp(), ther results still the same,( does split take care of the new line problem?)

modified code was attached..
Attachments: well.pl (0.58 KB)


FishMonger
Veteran / Moderator

Feb 3, 2009, 4:09 PM

Post #11 of 18 (2106 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

What is the real goal of this script?

What the the exact output you expect?

This is clearly a homework assignment, so I'll refrain from posting a complete and proper solution, but if you can answer those questions, I'll try to guide you.


robinson
Novice

Feb 3, 2009, 4:41 PM

Post #12 of 18 (2097 views)
Re: [FishMonger] why two same valued variables not equal ? [In reply to] Can't Post

Thanks a lot!

This is actually a code for analysing some experimental results, you may find that I'm not a real programming person.

for any plate well I need to match with the well number( in the input file: A003, A009...) so I can identify what position ( sequence ) those well located and get some experimental results from another data file.

So what I really need is to just match any input such as A009, A005 to those in the input file, but I just don't know why the they are not equal...

I need to do such analysis ASAP, Would appreaciate your help very much!


FishMonger
Veteran / Moderator

Feb 3, 2009, 4:52 PM

Post #13 of 18 (2095 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

Here are a few things for you to look into doing, which kind of goes against the grain of what I said I wasn't going to do.

Don't use -w in combination with the warnings pragma. It's better to use use the pragma instead of the -w switch.

It's better to use a lexical var for the filehandle instead of the bareword and you should be using the 3 arg form of the open call.

The die statement on the open call should include the reason it failed, which is stored in the $! var.

$. is one of the built-in Perl vars and it holds the current line number of the file you're looping over, which means it could take the place of your $i var.

You could clean up the while loop by using push and a list splice instead of @temp and the direct '=' assignment and you wouldn't need to use $i or $.


FishMonger
Veteran / Moderator

Feb 3, 2009, 4:57 PM

Post #14 of 18 (2090 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

You should load the data into a hash instead of an array and use A009, A005, etc as the keys. That would allow you to use it as a lookup table which make it much easier to match against the other data.

Based on your last comment, this sounds more work related than a class homework assignment. Which is it?


KevinR
Veteran


Feb 3, 2009, 5:18 PM

Post #15 of 18 (2087 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post

See if this helps you get startedL


Code
#!/usr/bin/perl 

use strict;
use warnings;
my $value = 'A009';
open ( IN, "<$ARGV[0]") or die ("can not open !");
while (<IN>) {
chomp;
my @temp = split(/\s+/);
if( $temp[1] eq $value ){
print "Found $value in the file!!!\n";
last; #stop searching file
}
}
close(IN);

-------------------------------------------------


robinson
Novice

Feb 3, 2009, 5:54 PM

Post #16 of 18 (2085 views)
Re: [KevinR] why two same valued variables not equal ? [In reply to] Can't Post

Many thanks to FishMonger and kevin.

the scripts Kevin provided return an error " Use of uninitialized value in string eq at well.pl line 10", don't know why. I checked that both $value and $temp[1] have correct string...

For the suggestion of FishMonger, I actually initially tried hash, but I was not able to get value by applying the inputed key (A009). I want to keep $i for a reference as the sequence of the well No.

It is not an assigment but work related, so I would very appreaciate if you may point out what is the main reason for causing such a problem.


FishMonger
Veteran / Moderator

Feb 3, 2009, 6:46 PM

Post #17 of 18 (2080 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $file = shift @ARGV || die "Usage: $0 <filename>\n";

my $value='A009';
my %file;

open my $FH, '<', $file, or die "Can't open '$file' $!";
while ( my $line = <$FH> ) {
chomp $line;
$file{ (split /\s+/, $line)[1] } = $.;
}

print "$value was found on line $file{$value} of $file\n" if exists $file{$value};



(This post was edited by FishMonger on Feb 3, 2009, 6:51 PM)


KevinR
Veteran


Feb 3, 2009, 7:21 PM

Post #18 of 18 (2073 views)
Re: [robinson] why two same valued variables not equal ? [In reply to] Can't Post


In Reply To
Many thanks to FishMonger and kevin.

the scripts Kevin provided return an error " Use of uninitialized value in string eq at well.pl line 10", don't know why. I checked that both $value and $temp[1] have correct string...


Your file may have blank lines or other odd lines. The "error" is actually a warning though and can probably be ignored for now. After you get the code working you can add filtering to avoid parsing blank lines or other lines that are not formatted properly.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives