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.

Recommended Answers

All 2 Replies

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.

Thanks Kevin. I read somthing on a site called www.pageresource.com about that issue. I usually use perldoc.perl.org for things specific to Perl. Guess it could have been a typo.

Steven.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.