my @interesting = ();
while(<DATA>) {
chomp;
push @interesting, $1 if m/^(?:attribute|value): (.*)$/;
}
my %attributes = @interesting; # magic
It's a kind of magic. :) You could even turn this into a oneliner, using e.g. grep and map, and employing some more dirty tricks along the way:
my %attributes = grep { length } map { m/^(?:attribute|value): (.*)$/
+and $1 } <DATA>;
Anyhow, going back to the while loop version, adding support for parsing the attributes of several objects at the same time is also fairly straightforward. E.g.:
my %dataObjs = ();
my @interesting = ();
my $dataObj;
while(<DATA>) {
chomp;
if(m/^AVUs defined for dataObj (.*):$/) {
defined $dataObj and $dataObjs{$dataObj} = { @interesting };
$dataObj = $1;
}
push @interesting, $1 if m/^(?:attribute|value): (.*)$/;
}
$dataObjs{$dataObj} = { @interesting };
Though in this case I'd use your solution instead, since it leads to much simpler code:
my %dataObjs = ();
my @interesting = ();
my $dataObj;
my $att;
while(<DATA>) {
chomp;
$dataObj = $1 if m/^AVUs defined for dataObj (.
+*):$/;
$att = $1 if m/^attribute:\s+(.+)/;
$dataObjs{$dataObj}->{$att} = $1 if /^value:\s+(.+)/;
}
No magic, alas. :)
Side note - according to my favorite WWW search engine, this sort of data is generated by iRODS. CPAN doesn't have any related modules, so here's a good opportunity to contribute to the Perl ecosystem for anyone who works with that system.
|