On Nov 28, 2:21 pm, paik...@googlemail.com (Dermot) wrote:
> 2009/11/28 raphael() <raphael.j...@gmail.com>:
>
>
>
> >> 2009/11/28 raphael() <raphael.j...@gmail.com>:
> >> > Hi,
> >> Hi,
>
> >> > # records.txt
> >> > 1111 25.11.2009 NAME_0
> >> > 2222 15.12.2006 NAME_3
> >> > 3333 20.10.2007 NAME_1
> >> > 1111 01.01.2008 NAME_3    <-- This whole line should be printed.
> >> > 4444 10.10.2008 NAME_4
>
> >> > Using while in a while loop matching ( m/1111/ ) I get all the entries
> >> > having "1111".
>
> >> What is distinctive about the line you are trying to print? Do you
> >> want the 4th line every time or is there some combination of 1111 and
> >> NAME_ that you need? Once you determine what is unique about the line
> >> your after, you'll be on the way.
> >> Dp.
>
> > No combination! Just the last entry of the record "1111"  which is given
> > through user input and changes each time the script is called.
> > The whole line then is parsed and given to another subroutine as shown in
> > the subroutine code.
>
> Here one option. Stick the hits into an array and print out the last element.
>
> #!/bin/perl
>
> use strict;
> use warnings;
>
> chomp (my $input = <STDIN>); #111
>
> my @hits;
> while (<DATA>) {
>     push @hits, $_ if /$input/;
>
> }

or

my @hits = grep /$input/, <DATA>;

> print $hits[-1];     # prints 1111 01.01.2008 NAME_3
>
> __DATA__
> 1111 25.11.2009 NAME_0
> 2222 15.12.2006 NAME_3
> 3333 20.10.2007 NAME_1
> 1111 01.01.2008 NAME_3
> 4444 10.10.2008 NAME_4
>
> Good luck,
> Dp.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to