From: seanjao on
Hi there:

I would like to use Perl to search a file containing data like the
following.
aaa01234 99.99 99.99 99.99
bb0567 99.99 99.99 99.99
ccccc89101 99.99 99.99 99.99

How can I let perl to match the first column if I type "bb567" without
the zero between "bb" and "567"?

Thanks
Sean

From: Glenn Jackman on
At 2006-12-18 03:21PM, "seanjao(a)yahoo.com" wrote:
> Hi there:
>
> I would like to use Perl to search a file containing data like the
> following.
> aaa01234 99.99 99.99 99.99
> bb0567 99.99 99.99 99.99
> ccccc89101 99.99 99.99 99.99
>
> How can I let perl to match the first column if I type "bb567" without
> the zero between "bb" and "567"?

Based on these requirements:

my $wanted = 'bb567';

while (<DATA>) {
if (/^$wanted\b/) {
print 'exact match >> ';
}
elsif (/^([a-z]+)0+(\d+)/ and $wanted eq "$1$2") {
print 'munged match>> ';
}
else {
print 'no match >> ';
}
print;
}

__DATA__
aaa01234 99.99 99.99 99.99
bb0567 99.99 99.99 99.99
ccccc89101 99.99 99.99 99.99


--
Glenn Jackman
Ulterior Designer
From: Dr.Ruud on
Glenn Jackman schreef:

> if (/^$wanted\b/) {

Safer:
if (/^\Q$wanted\E\b/) {

--
Affijn, Ruud

"Gewoon is een tijger."
From: Josef Moellers on
Glenn Jackman wrote:
> At 2006-12-18 03:21PM, "seanjao(a)yahoo.com" wrote:
>
>> Hi there:
>>
>> I would like to use Perl to search a file containing data like the
>> following.
>> aaa01234 99.99 99.99 99.99
>> bb0567 99.99 99.99 99.99
>> ccccc89101 99.99 99.99 99.99
>>
>> How can I let perl to match the first column if I type "bb567" without
>> the zero between "bb" and "567"?
>
>
> Based on these requirements:
>
> my $wanted = 'bb567';
>
> while (<DATA>) {
> if (/^$wanted\b/) {
> print 'exact match >> ';
> }
> elsif (/^([a-z]+)0+(\d+)/ and $wanted eq "$1$2") {

Based on the requirements "without *the* zero", that would be
elsif (/^([a-z]+)0?(\d+)/ and $wanted eq "$1$2") {

Personally, I would have made that
elsif (/^([a-z]+)0*(\d+)/ and $wanted eq "$1$2")

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

From: seanjao on
Thanks guys!
It helps.

Sean