in reply to Need Help in Array

This snippet makes a few assumptions and glosses over a few implementation details:

Given those assumptions the following snippet will parse the file and place each "module/id" pair into an anonymous array, with all of them pushed onto an array poorly named @results.

use strict; use warnings; use autodie; my $filename = 'something.doc'; my @results; open my $in_fh, '<', $filename; my( $module, $id ); while( my $line = <$in_fh> ) { chomp $line; if( $line =~ /^Module:\s+(\d+)/ ) { $module = $1; next; } if( $line =~ /^ID:(\d+)/ ) { $id = $1; next; } if( $line =~ /yes/ ) { push @results, [ $id, $module ]; } }

It may feel unclean allowing $module and $id to retain values from one iteration to the next, even after a 'no' in the 'Customer:' field. But as long as the input is well formed, $module and $id will always contain valid information at the moment a 'yes' is encountered.


Dave