On Thu, Feb 19, 2009 at 22:42, <[email protected]> wrote:
> I have a single input file with entries as follows:
>
> --snip--
> USER1 [20090101] note
> bla bla bla
> bla bla bla
>
> USER2 [20090104] note
> bla bla bla
> bla bla bla
>
> --snip--
>
> What I'm trying to do is create a single-argument script which displays
> all entries for a given user.
>
> So calling it as 'filter.pl user1' will print all entries for USER1.
snip
Your data is perfect for the paragraph mode* of readline:
#!/usr/bin/perl
use strict;
use warnings;
my $user = shift;
my $regex = qr/\A$user/;
local $/ = '';
while (my $record = <DATA>) {
chomp $record;
if ($record =~ /$regex/) {
print "$record\n";
last;
}
}
__DATA__
USER1 [20090101] note
bla bla bla
USER2 loves this user
USER2 [20090104] note
bla bla bla
bla bla bla
USER3 [20090107] note
bla bla bla
bla bla bla
* http://perldoc.perl.org/perlvar.html#$/
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/