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.

<-radiant.matrix->
Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

In reply to Re: regex help! by radiantmatrix
in thread regex help! by igotlongestname

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.