[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Wildcard



>Is there a way to use wildcards in perl? It would be nice if the folowing 
>would work:
>
>if ($text eq "t*") {print "$text\n"} # Where * is a wildcard
>
>So the print comand is executed when $text is "tee" or "time" or ...

Heh, heh.  Trust me, Thomas, this is rather like saying "it would 
be nice if I could use the mouse to click on things on my Mac's 
desktop."


Read up on Regular Expressions.  They're the cornerstone of perl.  
Well, one of them anyway.

  if ($text =~ /^t.*/) {print "$text\n"}

Does what you ask.  (But the .* is actually unnecessary here.
By the way, /t*/ would be zero or more occurrences of "t" 
anywhere in the string and will always be true.)
And, if you happen to have   $_ = "$text\n";   then

  print if /^t/;

So, for instance...

  my(@list) = ('tee', 'time out', 'Tea time', 'pteradactyl', 'toluene');
  for (@list) {
    print "$_\n" if /^t/;
  }

will give you the three items which begin with "t".

Enjoy,


-Charles

--
 <albrecht@gate.net>     <http://www.gate.net/~albrecht/cv/>



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch