I'm working with an API to get Information and save it into a file
#!/bin/perl
use strict;
use warnings;
use JSON;
use Data::Dumper;
my %Hash;
my @Array;
my $url = "https://foo/bar"
my $apidata = `/usr/bin/curl -k -u $url`
my $apidatajson = decode_json $apidata;
my $i=0;
foreach(@{ $apidatajson->{category} }){
@Array=();
my $Name=$apidatajson->{category}[$i]{Name};
my $value=$apidatajson->{category}[$i]{Value};
push(@Array,($Name,$value));
$Hash{$Name}=@Array; # Output 1
$Hash{$Name}=\@Array; # Output 2
$i++;
}
print Dumper \%Hash;
Output 1 looks something like this:
$VAR1{
"FooName2" => 2;
"FooName" => 2;
};
Output 2 looks something like this:
$VAR1{
"FooName2" => [
"FooName2"
"FooValue2"
]
"FooName" => $VAR1 -> {'FooName2'}
};
Id need it to look like this:
$VAR1 {
"FooName2" => [
"FooName2"
"FooValue2"
]
"FooName" => [
"FooName"
"FooValue"
]
}
So essentially what I'm asking is, how can I save the content of the Array in the Hash, without using a Reference, so it doesn't mess up every entry but the last one?
}on the line of theforeach. Futhermore, your code does not reproduce the issue you are talking about (probably because you added amybefore@Array=()within the loop). Finally, you are using double quotes instead of backticks aroundcurl https:/foo/bar. Next time, please test your code before posting a question.