0

I have a response which is displayed through Data::Dumper - Dumper($cat_response->result->{'categories'})

$VAR1 = { 'literature' => '1120', 'law' => '1153', 'arts and crafts' => '1132', 'children' => '1141', 'hobbies' => '1133', 'economy' => '1166', 'jobs' => '1140', 'media' => '1144', 'music' => '1147', 'animals' => '1170', 'business' => '1119', 'diet' => '1122', 'travel reviews' => '1154', 'jewelry' => '1157', 'movies' => '1146', 'television' => '1125', 'politics' => '1168', 'internet' => '1139', 'history' => '1129', 'recipes' => '1156', 'press releases' => '1151', 'presents' => '1128', 'marketing' => '1143', 'translations' => '1162', 'fashion' => '1145', 'technology' => '1163', 'real estate' => '1138', 'computer' => '1173', 'automobile' => '1116', 'finances' => '1126', 'weddings' => '1134', 'games' => '1127', 'esoterism' => '1124', 'horoscopes' => '1135', 'shopping' => '1123', 'humor' => '1137', 'miscellaneous' => '1159', 'science' => '1167', 'programming' => '1152', 'languages' => '1161', 'beauty' => '1117', 'sports' => '1160', 'hotels' => '1136', 'plants' => '1149', 'education' => '1118', 'traveling' => '1155', 'health' => '1130', 'telecommunication' => '1164', 'environment' => '1171', 'software' => '1158', 'sweepstakes' => '1131', 'logistics' => '1142', 'home and family' => '1169', 'news' => '1148' };

To access it, I use:

my %categories = $cat_response->result->{'categories'};
foreach my $cat (keys (%categories)) {
    <option value="<% $categories{'$cat'} %>"><% $cat %></option>
}

However, the value of Dumper($cat) is: $VAR1 = 'HASH(0x7fe972641560)';

Did I miss something?

0

1 Answer 1

2

You missing use strict; use warnings; for one. (Well, either that, or you forgot to tell us that Perl told you about your problem.)

$cat_response->result->{'categories'} contains a reference to a hash. Makes no sense to assign that to a hash.

my $categories = $cat_response->result->{'categories'};
foreach my $cat (keys (%$categories)) {
    <option value="<% $categories->{'$cat'} %>"><% $cat %></option>
}
Sign up to request clarification or add additional context in comments.

2 Comments

I apologize, I normally have strict and warnings enabled. Didn't think it was a problem. Thanks for the help, that did it.
I don't care if you actually include use strict; use warnings; in the code you post here, but I do expect you to use it. If you had, Perl would have complained about an odd number of values being assigned to a hash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.