Consider the following light weight comparison of various idioms:

use strict; use warnings; # Declare my ($website_1, $website_2, $website_3); my @sites; my %siteHash; # Populate vars $website_1 = 'www.site1.com'; $website_2 = 'www.site2.com'; $website_3 = 'www.site3.com'; # Populate array push @sites, "www.site$_.com" for 1 .. 3; # Populate hash $siteHash{"website_$_"} = "www.site$_.com" for 1 .. 3; # use individual sites print "Site 1 is $website_1\n"; print "Site 1 is $sites[0]\n"; print "Site 1 is $siteHash{website_1}\n\n"; # use site collections print "Site $_\n" for ($website_1, $website_2, $website_3); print "\n"; print "Site $_ is $sites[$_-1]\n" for 1 .. 3; print "\n"; print "Site $_ is $siteHash{$_}\n" for sort keys %siteHash;

Prints:

Site 1 is www.site1.com Site 1 is www.site1.com Site 1 is www.site1.com Site www.site1.com Site www.site2.com Site www.site3.com Site 1 is www.site1.com Site 2 is www.site2.com Site 3 is www.site3.com Site website_1 is www.site1.com Site website_2 is www.site2.com Site website_3 is www.site3.com

and note that individual variables don't really allow management as a collection (your problem) as both hashes and arrays do. Arrays don't give any help when accessing individual elements, but are suscinct. Hashes allow "symbolic" access to individual elements and management as a collection, but are a little more cumbersom.

You will perhaps have noticed that arrays and hashes of references have been omitted. If you are comfortable with those you probably wouldn't be asking the question. :)


DWIM is Perl's answer to Gödel

In reply to Re: looping through variables by GrandFather
in thread looping through variables by keiusui

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.