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

Hello great perlomnks! I have a big problem in reading some records from a textfile. I have the following text file:
------------------------------------------------------- RDN[ 01 ] : checking my test = 1 RDN[ 1 ] : allowing data = 11030101 RDN[ 2 ] : retrieving data = 1 FOR CLASS main WITH NAME TYPE help = < 1 > Testnok = < [0] [1] [0] [[0]] [[1]] [[0]] > ; --------------------------------------------------------------
And I'm interested in extracting the values between "Testnok = <" and ">;", exactly the following values:
[0] [1] [0] [[0]] [[1]] [[0]]
I have done this in this way, but it's wrong:
open(IN, "< myfile.txt"); my @list = <IN>; close IN; while ( $_ = shift ( @list ) ) { s/Testnok\s*=\s*<\s*//; s/\s*>//; s/\s*//; } #and it's not printing only my interested values
Thank you for your time

Replies are listed 'Best First'.
Re: reading from a textfile
by davidj (Priest) on Jun 14, 2004 at 14:06 UTC
    Use the range operators. This will get you started:

    open(FILE, "<myfile.txt"); while(<FILE>) { if(/Testnok/ .. /\>/) { print "$_"; } } close(FILE);

    hope this helps,
    davidj
      I have tried in all the ways but it still doesn't print anything. Thanks again for your help.
        The code I offered works with the text snippet you supplied. Therefore, you either have a mistake in your code or the text file you are working with is formatted differently. Please show us what you are doing, (give us the code), and a larger sample of the text file myfile.txt (all of it if possible) and we will be able to better help you.

        davidj
Re: reading from a textfile
by Happy-the-monk (Canon) on Jun 14, 2004 at 14:04 UTC

    This will do it:

    use Data::Dumper; + open(IN, "< myfile.txt") or die $! my $data_ok = 0; + while(<IN>) { chomp; + m/^\s*>\s*$/ and last; # stop when encountering that last ">". $data_ok and push @data, $_; # keep all data inbetween. m/Testnok/ and $data_ok = 1; # start after "Testnok". } print Dumper \@data;

    Cheers, Sören

Re: reading from a textfile
by Wonko the sane (Curate) on Jun 14, 2004 at 14:46 UTC
    Just a different way to do it.

    #!/usr/local/bin/perl -w use strict; my $data = do { local($/); <> }; print qq{$1\n} if ( $data =~ /(Testnok =[^>]+>)/s )

    Or all right on the command line. Prob a cleaner way to do this though,
    but I dont have my Camel book with me. Bet this could be done better with a couple more switches.

    perl -e 'print $1 if ( do{local $/; <>} =~ /(Testnok =[^>]+>)/s)' data +.txt

    Wonko