in reply to searching data lines between keywords
#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash; my $currentkey; my $inkey = 0; while (<DATA>) { chomp; next if /^\s*$/; # skip blank lines if ($inkey == 0) { $currentkey = $_; $inkey = 1; next; } if (/^\*+END\*+$/) { $inkey = 0; next; } push @{$hash{$currentkey}}, $_; } print Dumper(%hash), $/; __DATA__ mykeyword1 foo1 bar1 ***END*** mykeyword2 ***END*** mykeyword3 baz3 foo3 bar3 ***END*** mykeyword4 baz4 ***END***
Output:
$VAR1 = 'mykeyword3'; $VAR2 = [ 'baz3', 'foo3', 'bar3' ]; $VAR3 = 'mykeyword1'; $VAR4 = [ 'foo1', 'bar1' ]; $VAR5 = 'mykeyword4'; $VAR6 = [ 'baz4' ];
Lupey
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching data lines between keywords
by kaif (Friar) on Jun 14, 2005 at 15:37 UTC | |
by jhourcle (Prior) on Jun 14, 2005 at 16:32 UTC | |
by lupey (Monk) on Jun 14, 2005 at 16:50 UTC | |
by kaif (Friar) on Jun 14, 2005 at 16:57 UTC |