If using an array of products:
my @products; ... if (/^Product\sname:\s(\w.*)/) { push(@products, { name => $1, opts => [] }); } if (/^Option\sName:\s(\w.*)/) { push(@{$products[-1]{opts}}, $1); } ... foreach my $product (@products) { my $name = $product->{name}; my $opts = $product->{opts}; print($name, ': ', join(' ', @$opts), "\n"); }
If using a hash of products:
my %products; ... if (/^Product\sname:\s(\w.*)/) { $name = $1; $products{$name} = []; } if (/^Option\sName:\s(\w.*)/) { push(@{$products{$name}}, $1); } ... foreach my $name (keys %products) { my $opts = $products{$name}; print($name, ': ', join(' ', @$opts), "\n"); }
Update: Pluralized the array/hash name (@products/%products) to avoid confusion with $product.
Update: Added initialization code.
In reply to Re^3: Multi-dimensional Array Issue
by ikegami
in thread Multi-dimensional Array Issue
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |