develooper Front page | perl.beginners | Postings from February 2009

Re: breaking-apart and printing an input file

Thread Previous | Thread Next
From:
Chas. Owens
Date:
February 19, 2009 20:39
Subject:
Re: breaking-apart and printing an input file
Message ID:
58ce48dc0902192039q26bf1532n9d759232abe5a63d@mail.gmail.com
On Thu, Feb 19, 2009 at 22:42,  <pablo@compugenic.com> 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.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About