1 Replies - 906 Views - Last Post: 16 October 2008 - 12:02 AM

#1 xesecre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-July 08

Question for PERL if stament

Posted 15 October 2008 - 05:32 PM

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
Is This A Good Question/Topic? 0
  • +

Replies To: Question for PERL if stament

#2 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Question for PERL if stament

Posted 16 October 2008 - 12:02 AM

View Postxesecre, 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


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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1