Sara has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys , can someone give me an idea of how to do this : I have this file containing the following :
nbssbase/acn/bld/libacnsGH040.bld:22:acnss.cxx
nbssbase/acn/bld/libacnsGH040btsc.bld:23:acnss.cxx
nbssbase/acn/bld/libacnsGH040eel.bld:24:acnss.cxx
nbssbase/acn/bld/libacnsGH040mm.bld:22:acnss.cxx
nbssbase/acn/bld/libacnsGH060.bld:21:acnss.cxx
I would like to only capture one of the .bld files "not a spicific one" let's say libacnsGH40.bld thanks

edited: Mon Jul 8 14:49:06 2002 by jeffa - added pre tags

Replies are listed 'Best First'.
Re: parsing file
by fruiture (Curate) on Jul 08, 2002 at 14:59 UTC
    $ perl -ne 'm/(\w+\.bld)/ and print$1,$/ and last' your_file
    get's exactly the first .bld file
Re: parsing file
by Sifmole (Chaplain) on Jul 08, 2002 at 15:05 UTC
    I think the ingredients you need are a while loop, a regular expression and possibly an array to push your answers into.
    #!/usr/local/bin/perl -w use strict; my @hold; while (<DATA>) { push(@hold, $1) if (m/(\w+\.bld)/); } print join("\n", @hold), "\n"; __DATA__ nbssbase/acn/bld/libacnsGH040.bld:22:acnss.cxx foo nbssbase/acn/bld/libacnsGH040btsc.bld:23:acnss.cxx bar nbssbase/acn/bld/libacnsGH040eel.bld:24:acnss.cxx nbssbase/acn/bld/libacnsGH040mm.bld:22:acnss.cxx nbssbase/acn/bld/libacnsGH060.bld:21:acnss.cxx
    If you truly only want one, then just change
    push(@hold, $1) if (m/(\w+\.bld)/); to push(@hold, $1) && last if (m/(\w+\.bld)/);
Re: parsing file
by TexasTess (Beadle) on Jul 08, 2002 at 23:23 UTC
    You can do a while loop and load them into an array..then use the random number generator to randomly select a value from zero to max array index and thus select ONE of them...randomly...
    open(FILE,$FILEVARNAME); While(<FILE>){ (/^\s*$/) and next; chomp $_; push(@array,$_); } close(<FILE>); $SIZE=@array; $select = rand($SIZE); print @array[$select];


    TexasTess
    "Great Spirits Often Encounter Violent Opposition From Mediocre Minds" --Albert Einstein