Hello, I have a question about IF statements for perl
I have this IF statement that looks for one asterisk
if $a eq "*";
I want an IF statement to look into the field $a and tell me if it even finds one or more asterisks in that field, how do I go about this?
Thanks
Johnny
Question for PERL if stament
Page 1 of 11 Replies - 805 Views - Last Post: 16 October 2008 - 12:02 AM
Replies To: Question for PERL if stament
#2
Re: Question for PERL if stament
Posted 16 October 2008 - 12:02 AM
xesecre, on 15 Oct, 2008 - 05:32 PM, said:
Hello, I have a question about IF statements for perl
I have this IF statement that looks for one asterisk
if $a eq "*";
I want an IF statement to look into the field $a and tell me if it even finds one or more asterisks in that field, how do I go about this?
Thanks
Johnny
I have this IF statement that looks for one asterisk
if $a eq "*";
I want an IF statement to look into the field $a and tell me if it even finds one or more asterisks in that field, how do I go about this?
Thanks
Johnny
One way is a regular expression:
$a = 'foo*foo*foo';
if ($a =~ /\*/) {
print "Found * in $a";
}
Another way is the index() function:
$a = 'foo*foo*foo';
if (index($a,'*') > -1) {
print "Found * in $a";
}
Regular expressions are for finding patterns in strings, index() is for finding substrings in strings. index() is better for your particular question, its more efficient but less powerful than a regular expression.
Your particular example is a very simple "find a character in a string" question, which is not a pattern, its a substring. A pattern would be something like "find 2 consecutive digits of any value followed by any lower-case alpha character" in a string:
if ($a =~ /\d\d[a-z]/) {
print "found it";
}
This post has been edited by KevinADC: 16 October 2008 - 12:03 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|