jonnyw83:

You're already working with people on some answers, so this post is just a couple observations about your code.

First, I'm wondering if using a pair of hashes with identical keys is a good idea. It can be, but I can't tell without knowing more about your program. I tend to use a single hash, and add a level to handle the split. For example, if you're comparing the contents of a pair of files, rather than having a hash for each file, you could add keys (such as FILE1 and FILE2) for the different files after your primary key:

for my $key (keys %hash) { if (exists $hash{$key}{FILE1}) { if (exists $hash{$key}{FILE2}) { # Key exists in both files } else { # Key exists only in file 1 } } else { # Key exists only in file 2 } }

Second: if you choose a different indentation style, you wouldn't need the comments that tell you '#closes if loop' -- good use of whitespace can make it obvious. There are quite a few different indentation styles that can make it clear. For example, you can vertically align the closing curly brace with the structure that's being closed:

foreach my $key (keys (%hasha)) { if (my @temp = grep( /$key/, @prevarray ) ) { push(@{$hasha{$key}},$value); next; } else { push (@{$hashb{$key}},$value); next; } }

Another popular method is to vertically align the open and closing curly braces, either indented:

foreach my $key (keys (%hasha)) { if (my @temp = grep( /$key/, @prevarray ) ) { push(@{$hasha{$key}},$value); next; } else { push (@{$hashb{$key}},$value); next; } }

or not:

foreach my $key (keys (%hasha)) { if (my @temp = grep( /$key/, @prevarray ) ) { push(@{$hasha{$key}},$value); next; } else { push (@{$hashb{$key}},$value); next; } }

There are many variations, and plenty of religious wars over indentation style. Don't get caught up in the wars, just be consistent within each script. Most people can easily get used to any of the styles fairly quickly, so don't get too hung up on it.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Updating arrays within hashes by roboticus
in thread Updating arrays within hashes by jonnyw83

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.