Re: perl question
From: Craig A. Berry (craigberry_at_mac.com.spamfooler)
Date: 06/08/03
- Next message: bob smith: "Re: OT: CNN Story Not Favorable To Bush"
- Previous message: Bart Zorn: "Re: timezone rule"
- In reply to: Antony Wardle: "Re: perl question"
- Next in thread: Antony Wardle: "Re: perl question"
- Reply: Antony Wardle: "Re: perl question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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.
>
>
>
- Next message: bob smith: "Re: OT: CNN Story Not Favorable To Bush"
- Previous message: Bart Zorn: "Re: timezone rule"
- In reply to: Antony Wardle: "Re: perl question"
- Next in thread: Antony Wardle: "Re: perl question"
- Reply: Antony Wardle: "Re: perl question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|