http://qs1969.pair.com?node_id=35068


in reply to regexp's

Well, if it's in a file, I'd do this:
@ARGV = filename.txt; while(<>) { /^(\d)\s+time\s+\d{2}\:\d{2}\:\d{2}\s+(\d+)/; $hash{$1} = $2; }
-------------------update-------------- as per merlyn's advice: (godz it's hard not to try to make spaceballs jokes...)
while(<>) { if(/^(\d)\s+time\s+\d{2}\:\d{2}\:\d{2}\s+(\d+)/) { $hash{$1} = $2; } }
or didn't i read something about this possibly being acceptable:
while(<>) { { /^(\d)\s+time\s+\d{2}\:\d{2}\:\d{2}\s+(\d+)/; $hash{$1} = $2; } }
since the enclosing {} puts it in a seperate block? i fully expect to be waaaay off on this.

Replies are listed 'Best First'.
RE: Re: regexp's
by merlyn (Sage) on Oct 03, 2000 at 18:14 UTC
    Do not use $1 except after you've verified that the match was successful, else you will get the previous $1. That would not have been fatal here, but if you'd been accumulating rather than setting, you'd get some odd stuff.

    -- Randal L. Schwartz, Perl hacker