SPUG: reg exp's
Jim Flanagan
jimfl at tensegrity.net
Tue Sep 3 14:38:20 CDT 2002
On Tue, 3 Sep 2002, Baker, Stephen M wrote:
>
> I need to parse quickly through a file of the following format:
>
> heading1: data1.1
> heading2: data2.1
> heading3: data3.1
>
> heading1: data1.2
> heading2: data2.2
> heading3: data3.2
>
> heading1: data1.3
> heading2: data2.3
> heading3: data3.3
>
> to populate a data structure. Is regular expressions the way to go about
> this? OR does somebody have a trick for how this might be done?
Since it appears that the pattern is simple, you can use split here.
use FileHandle;
my %datahash;
my $fh = new FileHandle("<datafile");
die "Error opening file: $!\n" unless defined $fh;
while(my $line = <$fh>) {
next if $line =~ /^$/;
my ($k, $v) = split(/:\s+/, $line);
die "Error in datafile\n" unless data_seems_reasonable($k, $v);
push @{$datahash{$v}}, $v;
}
close $fh;
do_something_with($datahash{heading1}->[0]);
for my $h2 (@{$datahash{heading2}}) {
do_something_else($h2)
}
--
Flanagan::Jim
http://jimfl.tensegrity.net
mailto:jimfl%40t%65ns%65gr%69ty.n%65t
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list
mailing list