reading from a textfile
bory
created: 2004-06-14 09:57:02
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 = ;
  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
Re: reading from a textfile
created: 2004-06-14 10:04:42

This will do it:

use Data::Dumper;
                                                                                
open(IN, "< myfile.txt") or die $!

my $data_ok = 0;
                                                                                
while() {
  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
created: 2004-06-14 10:06:24
Use the range operators. This will get you started:

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

hope this helps,
davidj
Re^2: reading from a textfile
created: 2004-06-15 02:32:20
I have tried in all the ways but it still doesn't print anything. Thanks again for your help.
Re^3: reading from a textfile
created: 2004-06-15 07:33:02
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
created: 2004-06-14 10:46:16
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

perlmonks.org content © perlmonks.org and bory, davidj, Happy-the-monk, Wonko the sane

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03