On Fri, 17 Dec 2004 15:23:35 +0800, Khairul Azmi <[EMAIL PROTECTED]> wrote:
Hi, start your script with:
#!/usr/bin/perl -w
use strict;
> sub get_conf {
> my $local_file = $_[0];
> my @local_conf;
local_conf is an array. An assosiative array in Perl, is called a hash.
You declear it with:
my %hash;
> if (open (INFILE, "$local_file")) {
> while (<INFILE>) {
> my $line = $_;
>
> # if line is not a comment
> if ($line !~ /^#|^\s/) {
> my @conf_fields=split(/\s+/,$line);
> if ( $conf_fields[0] eq "var" ) {
> $local_conf[$conf_fields[1]] = $conf_fields[2];
This is array subscription. What you really want is:
hash{key} = value
Basically:
my %hash;
get various $keys and $values in loop
then set $hash{$key} = $value;
> }
> }
> }
> close (INFILE);
>
> print "A. $local_conf{'word1'} \n";
> print "B. $local_conf{'word2'} \n";
You can't use an array like that, and you shouldn't use a hash like
that. Hardcoding the 'keys' you are getting dynamically is poor
design.
This is better, or you could use Data::Dumper
foreach (keys %hash) {
print "$_ -> $hash{$_}\n"
}
Tor
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>