Re: perl question

From: Craig A. Berry (craigberry_at_mac.com.spamfooler)
Date: 06/08/03


Date: Sun, 08 Jun 2003 18:35:13 GMT

In <3ee2c054$0$26636$afc38c87@news.optusnet.com.au> Antony Wardle wrote:
> this how my script looks now:
>
> if (open(MYFILE, "file")) {
>
> $line = <MYFILE>;
>
> print ($line);
>
>
> # print ("Enter the search pattern:\n");
>
>
>
> $pattern = $line ;
>
>
> I guess I change the above to look like:
>
> $line=~s/^0//;
> $pattern = $line
>
>
> sound good?

If the goal is to strip a single leading '0' from the pattern you have
read in, then yes, that should work. It should be mentioned, though,
that an exact string match (particularly of a single character) is only
a "pattern" in the most trivial sense. It's not clear whether the
strings you want to match in the file passed in as an argument are exact
matches or true pattern matches. If they are exact matches, the index
function will be more efficient than regular expression matching. See

$ perldoc -f index

Regular expressions really shine, though, when the matching problem is a
bit more difficult. For example, if your phone numbers were at an
unknown position in a line of text and had differing conventions about
the use of parentheses around the area code and dashes between
components. For a gentle introduction to regular expressions, try:

$ perldoc perlretut

>
> print ($pattern);
>
> $filename = $ARGV[0];
>
> $linenum = $matchcount = 0;
>
> print ("Matches found:\n");
>
> while ($line = <>) {
>
> $linenum += 1;
>
> if ($line =~ /$pattern/) {
>
> etc.
>
>
>
>
> "Craig A. Berry" <craigberry@mac.com.spamfooler> wrote in message
> news:652dd8540b46003b23b7120f2970b48e@free.teranews.com...
>> In <3ee25ddf$0$31550$afc38c87@news.optusnet.com.au> Antony Wardle
>> wrote:
>> > I am struggling to figure out how to remove the first character off
>> > a string I am using in a pattern command.
>> >
>> > Ideally I'd like to see if the first character is a zero, then
>> > remove it.
>>
>> $ perl -e "$x='01234'; $x =~ s/^0//; print $x;"
>> 1234
>>
>> That says that if the contents of $x match the pattern '0' at the
>> beginning of the string, then substitute everything that matches with
>> nothing (or, in other words, remove the matching part of the string).
>>
>> > The problem I am trying to solve is to read a whole lot of phone
>> > numbers thaat are in a supplied file, then search for occurances of
>> > any of these numbers in another file.
>>
>> Perl is ideal for that sort of thing. You may find that the Perl
>> beginners list is a good source of help to get you started. It's
>> archived at
>>
>> http://nntp.x.perl.org/group/perl.beginners
>>
>> among other places. You'll probably also want a book; one of the
>> O'Reilly Perl books would be a good choice.
>
>
>



Relevant Pages

  • Re: Serious Perl Regular Expression deficiency?
    ... I started doing Perl 2 years ago and have ... > conclusion that regular expressions have a serious ... This is serious because the not string ... If you want to pull out the contents of XML comments you could do this. ...
    (comp.lang.perl.misc)
  • Serious Perl Regular Expression deficiency?
    ... After extensive read on the Perl docs on re's ... conclusion that regular expressions have a serious ... This is serious because the not string ... howdy folks --> ...
    (comp.lang.perl.misc)
  • Re: Serious Perl Regular Expression deficiency?
    ... I started doing Perl 2 years ago and have ... >> conclusion that regular expressions have a serious ... This is serious because the not string ... >If you want to pull out the contents of XML comments you could do this. ...
    (comp.lang.perl.misc)
  • Re: FAQ 6.11 Can I use Perl regular expressions to match balanced text?
    ... Can I use Perl regular expressions to match balanced text? ... Perl regular expressions were not capable of matching ... The perlfaq-workers, a group of volunteers, maintain the perlfaq. ...
    (comp.lang.perl.misc)
  • Re: Perl regrp 101 -- on OSX.....
    ... >> What version of Perl are running and on what OS ... recognized as matching \w. ... the entire string, but that /^\w/ isn't matching at all and the string ... ANSI/ASCII/utf8/utf16 conversion settings in there that may be ...
    (perl.beginners)