http://qs1969.pair.com?node_id=569459

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

Hello fellow monks;
I am trying to capture all of the individual SECTION/FIGURE/ITEM by column for each individual line. I can't read the file line by line because the data might run over to the next line. I tried to put the results of the regex into an array but that makes a separate entry in the array for each field. When I do it the way in the example I only get the first occurrence of SECTION/FIGURE/ITEM. What kind of looping structure do I need to get all of the occurrences within my data. Thanks
#!/pw/prod/svr4/bin/perl use strict; use warnings; undef $/; my $data = <DATA>; $data =~ m{(\d+-)(\d+-)(\d+)(\s*/\s*)(\d+)(\s*/\s*)(\d+)}g; print "$1, $2, $3, $4, $5, $6, $7\n"; __DATA__ PART NAME QUANTITY SECTION/FIGURE/ITEM Widget1 1 72-33-00/ 23/ 85 Widget1 1 75-20-00/ 11/ 10 Widget2 1 72-33-00/20/10 Widget2 1 72-33-00/20/70 Widget2 1 72-00-33/2/1 Widget2 1 72-00-33/2/20 Widget2 1 73-12-00/4/1 Widget2 1 73-12-00/4/220 Widget2 1 73-12-00/3/10 Widget2 2 79-20-00/9/1 Widget2 1 79-20-00/5/1
-- Grey Fox
Perl - Hours to learn lifetime to master.

Replies are listed 'Best First'.
Re: How to capture all occurances from a slurped file.
by shmem (Chancellor) on Aug 24, 2006 at 22:04 UTC
    You're just missing the loop:
    while ($data =~ m{(\d+-)(\d+-)(\d+)(\s*/\s*)(\d+)(\s*/\s*)(\d+)}g) { print "$1, $2, $3, $4, $5, $6, $7\n"; }

    After each match is found, the search continues where the last match left. So you get all occurrences.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks;
      I knew I was close. I just could not put my finger on what to use for the loop.
      -- Grey Fox
      Perl - Hours to learn lifetime to master.
Re: How to capture all occurances from a slurped file.
by imp (Priest) on Aug 24, 2006 at 22:14 UTC
    You were very close to solving the problem. You just need to repeat the search multiple times, as perl will continue the search when the last one ended.
    use strict; use warnings; use Data::Dumper; my $data; # Modifying $/ in a limited context to avoid # affecting other areas of the code. { local $/; $data = <DATA>; } # # Create the regular expression using qr{}x as it makes # the pattern much easier to follow (and maintain) # my $pattern = qr{ (\d+-) (\d+-) (\d+) (\s*/\s*) (\d+) (\s*/\s*) (\d+) }x; my @list; while ($data =~ /$pattern/g) { push @list,[$1,$2,$3,$4,$5,$6,$7]; } print Dumper \@list; __DATA__ PART NAME QUANTITY SECTION/FIGURE/ITEM Widget1 1 72-33-00/ 23/ 85 Widget1 1 75-20-00/ 11/ 10 Widget2 1 72-33-00/20/10 Widget2 1 72-33-00/20/70 Widget2 1 72-00-33/2/1 Widget2 1 72-00-33/2/20 Widget2 1 73-12-00/4/1 Widget2 1 73-12-00/4/220 Widget2 1 73-12-00/3/10 Widget2 2 79-20-00/9/1 Widget2 1 79-20-00/5/1
    Note that I defined the regex before the loop. This makes the logic easier to follow, particularly when you use the 'x' modifier.