Hi,
I have an array holding a mixture of numbers and single letter strings (e.g., {1, 2, 4, 5, A, B, C} ). I am using the following sub-function to search for a given letter (e.g., ‘Q’) but it only works when I search for the numbers. When I search for any string, I get the index corresponding to the letter ‘A’ only. Why is this?
sub find
{
my($var, @array);
$var =$_[0];
@array=@_[1..$#_];;
my $index = 0;
++$index until $array[$index] == $var or $index > $#array;
#print "@array\n";
#print "$array[$index]\n";
return $index;
}
$result = &find (‘Q', @fsg);
print "$result\n $fsg[$result]\n”; # returns index for the first non-
+integer regardless of what that item is
$result = &find (‘2', @fsg);
print "$result\n $fsg[$result]\n”; # returns correct index
$result = &find (‘102', @fsg);
print "$result\n $fsg[$result]\n”; # returns the length as the array a
+s the index - which is the value I check to see if a match occurred o
+r not.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.