0

Hello everyone. I've tried several times to use associative arrays in my Perl scripts, but each time I have got syntax errors I can't understand. Based on the tutorials I've read I have tried to do things like this:

sub test_function {
my($key, %table); %table = ('word1', 'Perl', 'word2', 'is', 'word3', 'great'); $key = "word3";
print $table('word1');
print $table("word2");
print $table($key);
}

Each syntax used above produces a compile error and I can't work out the correct useage. Could someone tell me what I'm doing wrong? Any help appriciated.

Steven.

2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by Mushy-pea
0

you're using the wrong brackets on your hash keys, you use {} not ()

sub test_function {
my($key, %table);
%table = ('word1', 'Perl', 'word2', 'is', 'word3', 'great');
$key = "word3";
print $table{'word1'};
print $table{"word2"};
print $table{$key};
}

not sure what tutorials you have read but if they are telling you to use () instead of {} for hash key look ups they are wrong.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.