Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Changing variable names in a loop

by superfrink (Curate)
on Oct 15, 2010 at 20:02 UTC ( #865557=note: print w/ replies, xml ) Need Help??


in reply to Changing variable names in a loop

I prefer to use hash table keys when the idea of dynamic variable names comes up.

#! /usr/bin/perl -w use strict; use Data::Dumper; my @fields = qw/ inc_albums inc_genres /; my %record; my $content = "inc_albums=asdf\n"; foreach my $field (@fields) { ($record{$field}) = $content=~/${field}=(.*?)\n/; } print Dumper(\%record);

The output is:
$VAR1 = { 'inc_genres' => undef, 'inc_albums' => 'asdf' };


Comment on Re: Changing variable names in a loop
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://865557]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2015-07-04 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls