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.

-- FloydATC

Time flies when you don't know what you're doing


In reply to Re: looping in 2d array by FloydATC
in thread looping in 2d array by Linguist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.