0

Iam trying to create a simple array KEY => VALUE from a json response, here's my results when I dump the array but the keys are not what Im excpecting:

$VAR1 = 'expectedvalue1';
$VAR2 = 'expectedvalue2';
$VAR3 = 'expectedvalue3';

and here's my code that I found some part of it here (some comments says that there's backslash missing)

my %result = ();
foreach my $row (@json_response){
  $result{ $row->{"json_key"} } = $row->{"json_value"};
}
print Dumper(%result);

While I'm trying to get

expectedkey1 = expectedvalue1
expectedkey2 = expectedvalue2
expectedkey3 = expectedvalue3

Edit : I made a mistake in the names of keys.

3
  • 3
    I didn't read the full article you've linked. What you're seeing as your output is Data::Dumper taking a list of values, and assuming each one is one variable passed in. Because hashes in Perl are essentially lists of key/value pairs, they get turned into an argument list when passed to Dumper, so you get this $VAR1 stuff. Run print Dumper \%result to pass a reference to Data::Dumper, and you'll see something like $VAR1 = { "foo" => "bar", ... }. But that doesn't explain why your output doesn't have the keys as variables. Please edit and include the JSON you are parsing, and how. Commented Jun 3, 2020 at 11:22
  • The json is correct, I printed in the loop and show the expected key values, and when I did the print Dumper \%result it showed the expected result O.o well post your answer I'll accept it.. thank you Commented Jun 3, 2020 at 11:29
  • 2
    The problem we couldn't help you was a typo in your question. That's why I asked to see the data. Your question is well written, but it was incomplete. In the future please include a minimal reproducible example in your questions. I didn't answer but wrote a guess in a comment because it was just that, a guess. Commented Jun 3, 2020 at 14:00

1 Answer 1

2

Are you trying to get key => value and key/value have the same values?

If you're looking for that, maybe this can help you

#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my @array_items = qw(expectedvalue1 expectedvalue2 expectedvalue3);
my %hash_example;

foreach my $value (@array_items) {
        push(@{$hash_example{$value}}, $value);
}

print Dumper(\%hash_example);

OUTPUT:

$VAR1 = {
          'expectedvalue2' => [
                                'expectedvalue2'
                              ],
          'expectedvalue1' => [
                                'expectedvalue1'
                              ],
          'expectedvalue3' => [
                                'expectedvalue3'
                              ]
        };
Sign up to request clarification or add additional context in comments.

1 Comment

My bad I made a mistake in the name of keys, it's corrected now. And I'm looking foy key => value not key => array, @simbabque answer did the trick thank you very much

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.