in reply to looping in 2d array
First of all, in most situations you'll find it much easier to use foreach instead of for. That way, you're letting Perl do all the tedious work with keeping track of indices for you.
Second, as pointed out already, I would consider using a hash instead of an array. Arrays are for looping, hashes are for looking stuff up. I have a very strong gut feeling a data structure containing words will be used mostly for lookups, so let's try to model the data in a way most suitable for the purpose, whatever it is.
Example 1
Consider a hash of hashes. Since I have no idea what the tags 'nn', 'nns' etc. are, I have given them generic names. If naming the tags makes no sense, please just skip this example.
my $words = { 'foo' => { named_property_x => 'nn', named_property_y => 'nns', }, 'bar' => { named_property_x => 'nvbg', named_property_y => 'np', }, }; my $lookup = 'foo'; print $words->{$lookup}->{'named_property_x'}; # Would print 'nn' foreach my $named_property (%{$words->{$lookup}}) { # This would print all the named properties for a specific word, in +no particular order print $named_property . ':' . $words->{$lookup}->{$named_property} . + "\n"; } # To add a new word and/or tag: $words->{'baz'}->{'named_property_z'} = 'nps'; # Do delete a tag delete $words->{'baz'}->{'named_property_z'}; # To check for a specific tag: if ( $words->{'baz'}->{'named_property_z'} eq 'nps' ) { ... }
Example 1
OK so let's say naming the tags makes no sense. Let's instead use a hash of hashes where the tags themselves are used as keys for easy lookup:
my $words = { 'foo' => { nn => 1, nns => 1, }, 'bar' => { nvbg => 1, np => 1, }, }; my $lookup = 'foo'; my $tag = 'nn'; print $words->{$lookup}->{$tag}; # Would print '1' foreach my $tag (keys %{$words->{$lookup}}) { # This would print all the tags for a specified word, in no particul +ar order print $tag . "\n"; } # To add a new word and/or tag: $words->{'baz'}->{'nps'} = 1; # To delete a tag: delete $words->{'baz'}->{'nps'}; # To check for a specific tag: if ( $words->{'baz'}->{'nps'} ) { ... }
Example 3
Now, in your OP the code seems to indicate that the first tag holds a special importance, so let's try a hash of arrays to preserve the order:
my $words = { 'foo' => [ 'nn', 'nns' ], 'bar' => [ 'nvbg', 'np' ], }; my $lookup = 'foo'; print $words->{$lookup}->[0]; # Would print 'nn', the first tag foreach my $tag (@{$words->{$lookup}}) { # This would print the tags for a specified word, in the order in wh +ich they were defined print $tag . "\n"; } # To add a new word and/or tag: push @{$words->{'baz'}}, 'nps'; # To delete a tag: @{$words->{'baz'}} = grep { $_ ne 'nps' } @{$words->{'baz'}}; # To check for a specific tag: if ( grep { $_ eq 'nps' } @{$words->{'baz'}} ) { ... }
Notice that deleting and testing for specific tags now became a little bit trickier.
NOTE: I have not tested this code so it may not even work properly, thus illustrating two points: 1) that arrays are trickier to work with than hashes and 2) don't simply pick one of these examples to work with, they're only meant to give you ideas for different ways to model your data until it fits your needs.
Your best fit is probably not one of these examples anyway, because only you know the actual meaning of your data and how it will be used.
Time flies when you don't know what you're doing
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: looping in 2d array
by ww (Archbishop) on Nov 02, 2014 at 12:41 UTC | |
by FloydATC (Deacon) on Nov 02, 2014 at 13:55 UTC | |
by ww (Archbishop) on Nov 02, 2014 at 15:33 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2014 at 21:51 UTC |