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

greetings monks I have a bit of a quandry in a program I am writing I need to be able to read data from a flat file into some scalars, but once i began to approach the problem my only idea left me with the name of my scalar in side of a scalar with no idea what to do with it.

i shall try to explain that again so that i am hopefully more clear. i have a file with data in it like $data = stuff. and i need to figure out how to assign stuff to $data without knowing it is $data that i am processing but knowing that the thing to the left of the equals sign is $data.

i could have a giant long sent of if statements but i would rather not do that as i have about 100 or so things to read in from this file every time my front end for the script begins, which while not frequently is occasional enough that i would rather not do it that way (and it would take a hell of a lot of time to code in)

so does anyone have an elegant solution or do i begin coding ifs?
thanks for your time
jcpunk


all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)

Replies are listed 'Best First'.
Re: dynamic scalar names?
by chromatic (Archbishop) on Aug 01, 2003 at 21:34 UTC

    Perl has hashes and arrays built in. Either of those can be appropriate, though a hash sounds better now. perldata and perldsc have more information.

    This is a common question among beginning programmers. It helped me to realize that variable names were only there for my benefit — the computer doesn't care what the names are. The aggregate data structures exist when you don't or can't know what the names are when you're writing the program. Hashes associate values with names. Arrays store lists. It takes some experience to know when either is appropriate, but once you realize that you don't need to know the actual name of a variable to use it, you'll find programming is much easier.

Re: dynamic scalar names?
by waswas-fng (Curate) on Aug 01, 2003 at 21:37 UTC
    Most of the time when you feel the ned to dynamicaly name vars you are really looking to use a hash.

    so if the data file looked like this:
    data=stuff you want in data
    data2=stuff you want in data2
    ...
    data5921=stuff you want in data 5921

    you may:
    open (INFILE, "datafile.txt") or die "oopsie can not open datafile.txt +! $1\n"; while (<INFILE>) { chomp; ($part1,$part2) = split /=/; $itsahash{"$part1"} = $part2; } then you can access what you thought was data in the OP as: print $itsahash{"data"};
    which will print "stuff you wanted in data"

    -Waswas
Re: dynamic scalar names?
by dga (Hermit) on Aug 01, 2003 at 21:41 UTC

    I would suggest a hash.

    my %file_stuff; while(<DATA>) { chop; my($k, $v)=split('='); $file_stuff{$k}=$v; } #later want to know what data was set to print "data was ", $file_stuff{data}, "\n"; __DATA__ data=test other=sample

    This keeps the names and keeps them tidy and doesn't pollute your name space. As an added advantage you can look through the keys to see what was defined.

    my @defined_stuff=keys %file_stuff;
Why it's stupid to `use a variable as a variable name'
by PodMaster (Abbot) on Aug 01, 2003 at 21:48 UTC
    You just have to read (and I gotta tell you about)

    Why it's stupid to `use a variable as a variable name' Part 1 2 3

    :)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

(jeffa) Re: dynamic scalar names?
by jeffa (Bishop) on Aug 02, 2003 at 13:20 UTC
    You may not even need to code anything to extract your data into a Perl hash, thanks to Config::General. Say that your data file (named foo.txt) looks like this:
    name = Joe
    job  = dishwasher
    rate = 6.50
    disp = grumpy
    
    Then the following code will parse it, and create the hash for you:
    use strict; use warnings; use Config::General; my $conf = Config::General->new('foo.txt'); my %hash = %{ $conf->{DefaultConfig} }; print $hash{name},"\n"; print $hash{job},"\n"; print $hash{rate},"\n"; print $hash{disp},"\n";

    Be sure and read the docs for more info.

    Config::General is not the only good Config module available on CPAN, i also recommend Config::Tiny, Config::IniFiles, and Config::Simple.

    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)
    
Re: dynamic scalar names?
by liz (Monsignor) on Aug 01, 2003 at 21:35 UTC
    Maybe you mean:
    use strict; { # variable variable stuff allowed in here no strict 'refs'; $name = 'foo'; $$name = $value; # does: $foo = $value } # no more variable variable stuff anymore
    Note that it is a good thing to always use strict and only loosen that when it's really needed.

    Liz

    Update: You're better off using a hash.