in reply to regex help!
Large data files mean slurping is probably bad. So, process a line, and if you got a match, process the next one differently. Here's one way, off the top of my head:
Assume a file where your columns are space-separated, and that looks like:
This is a nifty file, eh? NP Some U and some other Pu 32 40 1 30 20 123.1 -120 And some other stuff
You'll want the 32, 1, and -120. Since you have essentially columns, you'll use a regex and a split. So (untested):
use IO::File; my $file = IO::File->new; $file->open('< data.dat') or die("Can't read the source:$!"); until ($file->eof) { my $line = $file->getline(); # the regex below will find lines that start with 'NU ' # and have the other fields you want somewhere, surrounded # with spaces. YMMV. if ($line =~ /^NU \s .* \s U \s .* \s Pu \s/sx ) { # we want to get the values from the next line # first, we find the column indexes we want... my @col = split(qr/\s/s, $line); #split on whitespace my %index; for (0..@col-1) { $index{$1} = $_ if $col[$_] =~ /^(NU|U|Pu)$/; } # now we get the next line and split it into columns $line = $file->getline(); chomp($line); @col = split(qr/\s/s, $line); # we can safely reuse @col # now print the appropriate values using the indexes we captured +. foreach (keys %index) { printf "%3s = '%s'\n", $_, $col[$index{$_}]; } } # end of if } # end of until
I suggest that your file is probably not as ugly; if you post a sample of the file with a clearer description, I bet I (or someone) could come up with more elegant code.
|
|---|