[PLUG] Perl question
Colin Kuskie
ckuskie at dalsemi.com
Mon Jun 23 15:09:01 UTC 2003
On Mon, Jun 23, 2003 at 02:45:54PM -0700, Matt Alexander wrote:
> I'm still learning my way around Perl... Here's what I want to do... I
> have a file that contains long lines of gibberish and buried in each line
> is a phone number (xxx xxxx). I would like to create an array that holds
> just the phone numbers. So I think I should do something like this:
>
> $i = 0;
> while (<INFILE>) {
> $phone[$i] = /\d\d\d \d\d\d\d/;
> $i++;
> }
>
> This, of course, doesn't work... Any suggestions?
It all boils down to 1 line:
$phone[$i] = /\d\d\d \d\d\d\d/;
Here's what it does.
// is the match operator. By default it matches against $_ and it
returns true for a match and false if it doesn't. So in the end your
array should hold a bunch of 1's and 0's, and the array index would
be the which line the phone numbers could be found on.
Here's what you'd like to do:
@phone = ();
while (<INFILE>) {
if (/\d\d\d \d\d\d\d/) {
push @phone, ""something"";
}
}
The push function treats @phone like a stack, and "pushes" the
2nd argument ""something"" onto the end of the array.
perldoc -f push
Array indexing is implicit, and there is a complementary function
called pop, which removes elements from the end of the array. There
are also orthogonal functions, unshift and shift, for working with the
beginning of the array so that you can create queues, FIFOs and LIFOs.
But how do you get ""something"" to be the phone number?
First, you must know how perl's regexen work.
perldoc perlre
perldoc perlop
You use parentheses to grab data from a regex, and when using the match
operator, if it's called in list context, then it returns all the data
captured by parentheses:
So here's the code:
use strict;
@phone = ();
my $phone;
while (<INFILE>) {
if (($phone) = /(\d\d\d \d\d\d\d)/) {
push @phone, $phone;
}
}
You could also do this using a little more cryptically with the
$1, $2, variables. (again, perlop and perlre)
use strict;
@phone = ();
while (<INFILE>) {
push @phone, $1 if /(\d\d\d \d\d\d\d)/;
}
But using the $1 will cost your code some speed, if that's a
constraint. Perl will let you constrain it just about any way you
want.
Colin
More information about the PLUG
mailing list