Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] testing the start of a string..

Expand Messages
  • Charles K. Clarkson
    Franki ... Use substr is faster, but I prefer regular expressions: my $my_var = a:bigLongString ; if ($my_var =~ /^(a:big)/i) { #
    Message 1 of 1 , Jul 1, 2001
    • 0 Attachment
      Franki <frankieh@...>

      : Hi again all..
      :
      : I have another small question,
      :
      : if I have a variable,, say: $myVar="a:bigLongString"
      :
      : and I want to test for the presense of a:big at the
      : start of it.. (and it must allow for either upper or
      : lower case)
      :
      : what would be the easiest way of doing that?
      : should I use split to take off the chars 0 though 4
      : and use an if/or statement to compare it to a:big
      : or A:BIG ?
      : what happens then if its mixed case? how can I tell it
      : to ignore case altogether?

      Use substr is faster, but I prefer regular
      expressions:
      my $my_var = 'a:bigLongString';
      if ($my_var =~ /^(a:big)/i) {
      # this is a case-insensitve match
      # see perlre for lots more info
      # $1 contains the exact match
      } else {
      # this line failed
      }

      : also, if I want to test for the presence of String
      : and remove it if its there, do I use the same method?

      Try substituion (see perlfunc and perlre):
      if ($my_var =~ s/^(a:big)//i) {
      # $1 contains the match and
      # $my_var = 'LongString'
      }
      :
      : I am trying to find a relivent section in my perl books,
      : but I have read 40 pages so far and haven't found
      : anything relivent..

      Look in perlfunc under 'Perl Functions by Category',
      then look under the heading: 'Functions for SCALARs or
      strings'. Play with substr as well as m// and s///.

      HTH,
      Charles K. Clarkson
      Clarkson Energy Homes, Inc.
    Your message has been successfully submitted and would be delivered to recipients shortly.