Click here to get back home

compare array element

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
compare array element Rose 02-21-2008
Posted by Rose on February 21, 2008, 10:28 pm
Please log in for more thread options
I find the following codes will give match for 'C' to '-', what's the
problem?

$preseq = "C-";
@preseq = splilt '', $preseq;

$curseq = "--";
@curseq = splilt '', $curseq;

$len = @curseq;
$len--;

if ($preseq[$len-1] == $curseq[$len-1]) {
print "P:$preseq[$len-1]\n";
print "C:$curseq[$len-1]\n";
}




Posted by Bob Walton on February 21, 2008, 10:51 pm
Please log in for more thread options
Rose wrote:
> I find the following codes will give match for 'C' to '-', what's the
> problem?
>
> $preseq = "C-";
> @preseq = splilt '', $preseq;
>
> $curseq = "--";
> @curseq = splilt '', $curseq;
>
> $len = @curseq;
> $len--;
>
> if ($preseq[$len-1] == $curseq[$len-1]) {
------------------------------^^
> print "P:$preseq[$len-1]\n";
> print "C:$curseq[$len-1]\n";
> }

One problem is you are using the numeric comparison operator "==" where
you should be using the string comparison operator "eq". The "=="
operator coerces both arguments to numbers, which means 0 for strings
that don't make sense as numbers.

HTH
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

Posted by Gunnar Hjalmarsson on February 21, 2008, 11:01 pm
Please log in for more thread options
Rose wrote:
> I find the following codes will give match for 'C' to '-', what's the
> problem?
>
> $preseq = "C-";
> @preseq = splilt '', $preseq;
>
> $curseq = "--";
> @curseq = splilt '', $curseq;
>
> $len = @curseq;
> $len--;
>
> if ($preseq[$len-1] == $curseq[$len-1]) {
> print "P:$preseq[$len-1]\n";
> print "C:$curseq[$len-1]\n";
> }

The problem is that you ask for help with debugging, while you haven't
first asked Perl for help by using strictures and warnings.

Add

use strict;
use warnings;

to the beginning of the script, and make the adjustments needed to get
rid of possible error and warning messages.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Rose on February 22, 2008, 12:14 am
Please log in for more thread options
Thanks to both Bob Walton's explanation and Gunnar Hjalmarsson's critics. I
feel very sorry for raising such stupid questions although I am very
hopeless to be ordered to learn several languages at the same time. I will
pay more attention to use strict and use warnings next time



Similar ThreadsPosted
Deleting array element does not change the array size. October 22, 2007, 9:39 pm
array::Compare - Perl extension for comparing arrays March 23, 2007, 5:32 am
Can I compare array with array? July 16, 2006, 7:30 am
Help with the last element in an array September 12, 2006, 2:54 pm
Get the index of array element September 13, 2004, 4:07 am
extract every other element from an array September 1, 2005, 2:25 pm
Picking Element from Array one by one November 30, 2005, 7:14 am
Help with delete an element in array September 11, 2006, 2:51 pm
finding the last element in a referenced array October 5, 2004, 9:37 am
FAQ: How can I tell whether a certain element is contained in a list or array? October 12, 2004, 11:10 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap