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. :)
In reply to Re: looping through variables
by GrandFather
in thread looping through variables
by keiusui
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |