c: this is actually a complicated question and is dependant on your program's particular needs. First, you have to ask yourself: "how frequently is this occurring?". If this was merely a constant value, such as PI, then yes, I'd extract it out and declare it elsewhere if I used it more than once. However, if this is part of another variable and you only have two keys referencing it, well, you may be adding complexity for a false benefit.

One key test is a question I always ask myself when I designing a database schema: are my data elements related, or merely similar? What if I have five guys named John who live at the same address? Their addresses are related, so properly they might need to be in another table. Their first names, however, are similar, but not related. I wouldn't want to abstract those out into another table. You have the same issue here. Is that prefix a coincidence or not? If it is and you abstract it out, you're shooting yourself in the foot. If you only have a couple of hash elements, you may be shooting yourself in the foot. If you have hundreds of them and they are related, you're doing yourself a favor:

my $prefix = 'abcd'; my %hash = ( a => 1234, b => 5678, . . . zzz => 666 ); %hash = map { $_, $prefix.$hash{$_} } keys %hash;

That, I think, is a win. Otherwise, it may be more trouble than it's worth. Of course, if you're adding the hash keys in several different places, then the prefix idea might very well be a good thing even if you do only have a couple of items.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Is a global variable better than repetition within variables? by Ovid
in thread Is a global variable better than repetition within variables? by c

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.