Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Storing an array of variables

by CodeJunkie (Monk)
on May 20, 2003 at 18:21 UTC ( #259531=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
This might seem like a stupid question so i'm sorry if it's been asked before, but I want to be able to store an array of variables, then loop though all these variables and perform a simple substition.

I thought the code would look something like this:

my @elements=(\$hall_name,\$contact_name,\$contact_email,\$contact_tel +ephone); foreach my $line (@elements) { print "$line<br/>"; $line=~s/'//g; }

This was a pretty wild guess because i'm not really used to using references, so I think i'm probably quite wide of the mark, but can anyone tell me how to do this,or a better way if there is one?

cheers,
Tom

Replies are listed 'Best First'.
Re: Storing an array of variables
by Mr. Muskrat (Canon) on May 20, 2003 at 18:32 UTC
    Simply dereference them...
    foreach my $line (@elements) { print "$$line<br/>"; # you sure that you don't want this after the s +ubstitution? $$line=~s/'//g; }
    Lots of good info on references here at the Monastery but look first to the perldocs.
Re: Storing an array of variables
by pzbagel (Chaplain) on May 20, 2003 at 18:43 UTC

    You can use map, I've set this up two ways, the @elements array uses the references the way you use them in your code. I call map to do the substition, at that point you can print it out however you like, I used Data::Dumper so you can see what the var structure looks like. In the second example @elems2 I did away with the references, with subtle changes to the map function(I do not dereference $_) BTW, $_ can be dropped at that point, but I wanted to keep it symmetrical with the other map. Notice how the first Data::Dumper output shows that the elements array contains references to the strings and the second contains the strings.

    #!/usr/bin/perl use Data::Dumper; $hall_name="Foo's Hall"; $contact_name="Prof Bar"; $contact_email="profbar\@fooshall.edu"; $contact_telephone="1'234'567'8901"; my @elements = ( \$hall_name, \$contact_name, \$contact_email, \$contact_telephone ); my @elems2 = ( $hall_name, $contact_name, $contact_email, $contact_telephone ); map { $$_ =~ s/'//g } @elements; print Dumper(\@elements); map { $_ =~ s/'//g } @elems2; print Dumper(\@elems2); __END__ #Output looks like: $VAR1 = [ \'Foos Hall', \'Prof Bar', \'profbar@fooshall.edu', \'12345678901' ]; $VAR1 = [ 'Foos Hall', 'Prof Bar', 'profbar@fooshall.edu', '12345678901' ]; # As you can see the first one contains the references, # And the second does not.

    HTH

Re: Storing an array of variables
by omnibus (Scribe) on May 20, 2003 at 18:36 UTC
    As far as handling references goes, you're almost spot on. The only thing you need to do is dereference $line in the loop where you're printing it out, by putting another $ in front of it like this:
    foreach my $line (@elements) { print "$$line<br/>"; ... }
    This is because the $line that gets created in the foreach my $line (@elements) bit isn't a scalar. It's a reference to a scalar (which makes sense because that's what's stored in @elements).
    Just as an aside, I'm not sure you even need to be storing references in the first place. Why not just store the actual values of $hall_name, etc. in @elements? Unless you're planning to modify them between putting them in the array and printing them out, storing the values would work just as well with the added bonus of making your code slightly easier to follow.
Re: Storing an array of variables
by cees (Curate) on May 20, 2003 at 19:01 UTC

    The answers above seem spot on in explaining where the problem is in your code, so I won't repeat their solutions here. Might I suggest you try a different approach than storing scalar references in an array though. What you are describing here seems a perfect opportunity to use a hash structure instead of scalar vars and an array. Try something like this (untested) code instead:

    my %elements = ( hall_name => $hall_name, contact_name => $contact_name, contact_email => $contact_email, contact_tel => $contact_tel, ); while (my($key, $value) = each %elements) { print "$key -> $value<br />"; $value =~ s/'//g; } - or - foreach my $line (values %elements) { ... }

    By using a hash, you are keeping all your data in one structure making the code clearer, and it makes it easier to pass this information to other methods or subroutines.

    The only real issue here is that hashes don't preserve order like arrays do, so if you require a specific order when printing these values you may need to look at alternative solutions (like Tie::IxHash). But since it looks like you are outputting HTML, you should really look at a templating system like HTML::Template or Template Toolkit which work well with hashes of data.

Re: Storing an array of variables
by fglock (Vicar) on May 20, 2003 at 19:20 UTC
      my @elements=(\$hall_name,\$contact_name,\$contact_email,\$contact_telephone);

    you can also use this idiom:

      my @elements = \( $hall_name, $contact_name, $contact_email, $contact_telephone );
Re: Storing an array of variables
by Jenda (Abbot) on May 20, 2003 at 21:53 UTC

    Do you realy need the array?

    foreach my $line ($hall_name, $contact_name, $contact_email, $contact_ +telephone) { print "$line<br/>"; $line=~s/'//g; }

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      Lots of great ideas, thanks. I actually ended up using this one because it seemed the simpliest and worked ;-)

      Cheers,
      Tom.

(jeffa) Re: Storing an array of variables
by jeffa (Bishop) on May 21, 2003 at 05:53 UTC
    You don't need references to do this:
    my @element = ( $hall_name, $contact_name, $contact_email, $contact_telephone ); s/'//g for @element; print map "$_<br/>", @element;
    Some might not like using two loops when this could be done with one, but i remind them that these two processes (filtering and printing) are best kept seperated anyway.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2020-05-25 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (143 votes). Check out past polls.

    Notices?