Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

looping through variables

by keiusui (Monk)
on Jul 04, 2006 at 02:50 UTC ( #559074=perlquestion: print w/replies, xml ) Need Help??

keiusui has asked for the wisdom of the Perl Monks concerning the following question:

I have five variables called $website_1, $website_2, $website_3, $website_4 and $website_5.

Is there a way to loop through these variables?

I tried the following to no avail:

for($x = 1; $x <= 5; $x++) { print $website_$x; }

Any help would be gratefully appreciated. Thank you.

Replies are listed 'Best First'.
Re: looping through variables
by davidrw (Prior) on Jul 04, 2006 at 02:55 UTC
    yes, but you're probably better off using an array or a hash (i had a discussion on this topic earlier today)..
    my @websites; print "Website = $_\n" for @websites;
    How are your variables currently being declared & populated?
      Also, you might consider this node: Here for a good description of why it is common, but a bad idea to do this kind of thing. I would just use an array for this, since it will probably not always be, say, 5 websites, but probably n. And if not, then just to get a bit of practice in writing good code (good habits build themselves, after a while).
Re: looping through variables
by GrandFather (Sage) on Jul 04, 2006 at 03:29 UTC

    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 = ''; $website_2 = ''; $website_3 = ''; # Populate array push @sites, "$" for 1 .. 3; # Populate hash $siteHash{"website_$_"} = "$" 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;


    Site 1 is Site 1 is Site 1 is Site Site Site Site 1 is Site 2 is Site 3 is Site website_1 is Site website_2 is Site website_3 is

    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

      print "Site $_ is $sites[$_-1]\n" for 1 .. 3;
      would be better written as
      print "Site $_ is $sites[$_-1]\n" for 1 .. @sites;
      print "Site ", $_+1, " is $sites[$_]\n" for 0 .. $#sites;

      Noone said the index needed to be printed:
      print "$_\n" foreach @sites;

Re: looping through variables
by kwaping (Priest) on Jul 04, 2006 at 03:19 UTC
    I agree that an array would be a much better tool for this job. However, with that said, I'd like to show you how it could be done, if you were dead-set on doing it that way.
    use strict; use warnings; my $website_1 = 'a'; my $website_2 = 'e'; my $website_3 = 'i'; my $website_4 = 'o'; my $website_5 = 'u'; for (my $x = 1; $x <= 5; $x++) { print eval "\$website_$x"; }
    This is neither good nor safe, but I just wanted to show that it was indeed possible.

    It's all fine and dandy until someone has to look at the code.
    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://559074]
Approved by davidrw
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2020-07-08 04:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found