Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Reading from an INI file

by suaveant (Parson)
on May 15, 2001 at 18:47 UTC ( [id://80544]=note: print w/replies, xml ) Need Help??


in reply to Reading from an INI file

yes, there is, you can do
${'STR1'} = 'STR2';
to assign string STR2 to $STR1, however, you are absolutely right that a hash can accomplish what you want, and is in fact a much better way to do it than assigning random variables, especially since it prevents you from overwriting your own program variables :)
The way I would do it is this...
my %info; for(@file) { next if /^#/; skip line if it starts with a hash chomp; # remove \n Update ACK! forgot this! Doh! my($name,$val) = split '=', $_, 2; #split line into two # values, on an = sign next unless $val; # make sure you got something $info{$name} = $val; } print "$info{OFFICE} - $info{NAME}\n";
very simple. you can also check for values by doing something like
if(exists $info{OFFICE}) { print "Ofiice: $info{OFFICE}\n"; }
and you can get a list of all your info variables by doing
@keys = keys %info;
try it, and use this to see the results...
for(sort keys %info) { print "$_: $info{$_}\n"; }
That will print a sorted list of all your keys with their values Update forgot to chomp... fixed it... oops
                - Ant

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found