Quoth nos...@lisse.NA:
>
> I have an anonymous hash with two elements and can print
> them out like so.
>
> foreach my $record (@$hash_records) {
> print "$record->{A},$record->{B}\n";
> }
>
> However, I have several records of A with variable numbers of B (n=1 to
> 5) and would like
>
> print one line of each A with B1, B2, B3, ..., Bn
It's not clear from this what your data structure looks like. Is it
something like
$hash_records = [
{ A => "foo", B1 => "bar", B2 => "baz" },
{ A => "blib", B1 => "blab", B3 => "blob" },
];
or something else?
If that's the right structure, then you want to reorganise it so it
looks like
$hash_records = [
{ A => "foo", B => ["bar", "baz"] },
{ A => "blib", B => ["blab", "blob"] },
];
since it will make manipulating it much easier. As a general rule of
thumb, when you start using names ending in numbers, you should probably
be using an array instead.
It would be best to do this reorganisztion at the point where you create
the data structure, since it better represents the structure of the
data, but if necessary you can use something like
for my $record (@$hash_records) {
my $A = $record->{A};
my @B = map $record->{$_},
sort
grep /^B/,
keys %$record;
print "$A, " . join ", ", @B;
}
to pull the keys you need out afterwards.
Ben