I have two array of hashes .Both have similar values in them but i want to create new key in the hash that will have the some values of second array of hash.
First Array:
[
{ area_code => 93, name => 'Afghanistan', code => 'AF', slno => 4554 },
{ area_code => 1684, name => 'American Samoa', code => 'AS', slno => 4557 },
];
Second Array:
[
{ city => "Berat", country => "AS", id => 134368 },
{ city => "Durres", country => "AS", id => 138466 },
{ city => "Kabul", country => "AF", id => 142462 },
];
Now in the first hash i have key code whose value is similar to the second hash key country .So i want to add a new key in the second array of hash which will be country_name.And the country_name value will be the value of first array of hash name.
So how can we do this please help me in this
use strict;
use warnings;
my $a1 = [
{ area_code => 93, code => "AF", name => "Afghanistan", slno => 4554 },
{ area_code => 1684, code => "AS", name => "American Samoa", slno => 4557 },
];
my $a2 = [
{ city => "Berat", country => "AS", id => 134368 },
{ city => "Durres", country => "AS", id => 138466 },
{ city => "Kabul", country => "AF", id => 142462 },
];
my %h = map { $_->{code} => $_ } #$a1;
for my $v (#$a2) {
$v->{country_name} = $h{ $v->{country} }{name};
}
This is a similar idea to #mpapec's, but, I think, a little cleaner.
use strict;
use warnings;
my #array1 = (
{ area_code => 93, name => 'Afghanistan', code => 'AF', slno => 4554 },
{ area_code => 1684, name => 'American Samoa', code => 'AS', slno => 4557 },
);
my #array2 = (
{ country => 'AS', city => 'Berat', id => 134368 },
{ country => 'AS', city => 'Durres', id => 138466 },
{ country => 'AF', city => 'Kabul', id => 142462 },
);
{
my %names = map { $_->{code} => $_->{name} } #array1;
$_->{country_name} = $names{ $_->{country} } for #array2;
}
use Data::Dump;
dd \#array2;
output
[
{
country => 'AS',
city => 'Berat',
id => 134368,
country_name => 'American Samoa',
},
{
country => 'AS',
city => 'Durres',
id => 138466,
country_name => 'American Samoa',
},
{
country => 'AF',
city => 'Kabul',
id => 142462,
country_name => 'Afghanistan',
},
]
Related
The big problem that I can't seem to figure out.
I need to input into this complex hash (array?):
my $raterequest =
{
Shipment =>
{
Shipper =>
{
(static data here)
},
ShipTo =>
{
(static data here too)
},
Package =>
[
{
PackagingType =>
{
Code => '02',
Description => 'Package'
},
PackageWeight =>
{
UnitOfMeasurement =>
{
Code => 'LBS'
},
Weight => $boxWt
},
},
{
PackagingType =>
{
Code => '02',
Description => 'Package'
},
PackageWeight =>
{
UnitOfMeasurement =>
{
Code => 'LBS'
},
Weight => $boxWt
},
}
],
}
};
What I need to input is everything inside the Package array.
I have an LoH that generates an output like this:
my %carton_specs =
(
25 => {
boxQty => 25,
boxWt => 4,
boxNo => 2
},
50 => {
boxQty => 50,
boxWt => 8,
boxNo => 17
},
);
Where I need to repeat the anonymous array inside Package
{
PackagingType =>
{
Code => '02',
Description => 'Package'
},
PackageWeight =>
{
UnitOfMeasurement =>
{
Code => 'LBS'
},
Weight => $boxWt
},
},
times the number returned from $boxNo. The only variable that changes in that is the Weight => $boxWt
Please excuse anything that I might have named wrong. I have been fighting with this for 2 days and my head is exploding.
It sounds to me like you are looking for push plus the information from perlreftut (plus maybe the .. range operator). I hope I understood your specifications correctly:
use warnings;
use strict;
use Data::Dump;
my $raterequest = {
Shipment => {
Package => [ ],
} };
my %carton_specs = (
25 => { boxQty => 25, boxWt => 4, boxNo => 2 },
50 => { boxQty => 50, boxWt => 8, boxNo => 17 },
);
for my $carton (sort keys %carton_specs) {
for ( 1 .. $carton_specs{$carton}{boxNo} ) {
push #{ $raterequest->{Shipment}{Package} }, {
PackagingType => {
Code => '02',
Description => 'Package',
},
PackageWeight => {
UnitOfMeasurement => { Code => 'LBS' },
Weight => $carton_specs{$carton}{boxWt},
},
};
}
}
dd $raterequest;
Output:
{
Shipment => {
Package => [
{
PackageWeight => { UnitOfMeasurement => { Code => "LBS" }, Weight => 4 },
PackagingType => { Code => "02", Description => "Package" },
},
{
PackageWeight => { UnitOfMeasurement => { Code => "LBS" }, Weight => 4 },
PackagingType => { Code => "02", Description => "Package" },
},
{
PackageWeight => { UnitOfMeasurement => { Code => "LBS" }, Weight => 8 },
PackagingType => { Code => "02", Description => "Package" },
},
# ... omit 16 repetitions of the previous hashref ...
],
},
}
I'm working in Laravel. I have two arrays, and I want to insert the second array as a value in the first. For example, given
$firstArray = [
'id' => 1,
'propery_name' => 'Test Property'
];
$secondArray = [
'id' => 2,
'user_name' => 'john'
];
, I want to produce an equivalent of
$resultArray = [
'id' => 1,
'propery_name' => 'Test Property',
'userData' => [
'id' => 2,
'user_name' => 'john'
]
];
How can I achieve that?
$resultArray = $firstArray;
$resultArray['userData'] = $secondArray;
Try this:
$firstArray = ['id'=>1,'propery_name'=>'Test Property'];
$secondArray = ['id'=>2,'user_name'=>'john'];
$resultArray = ['property' => $firstArray, 'userData' => $secondArray];
Your newly created array should give you this:
{{ $resultArray['userData']['user_name'] }} = John
{{ $resultArray['property']['propery_name'] }} = Test Property
$firstArray['userData'] = $secondArray;
return $firstArray;
//result
$resultArray = [
'id' => 1,
'propery_name' => 'Test Property',
'userData' => [
'id' => 2,
'user_name' => 'john'
]
];
You can use Laravel collection class for this. Push function is best way to add values in array. https://laravel.com/docs/5.5/collections#method-put
In your case,
$firstArray = [
'id' => 1,
'propery_name' => 'Test Property'
];
$secondArray = [
'id' => 2,
'user_name' => 'john'
];
$collection = collect($firstArray);
$collection->put('userData', $secondArray);
$collection->all();
Output:
['id' => 1,
'propery_name' => 'Test Property',
'userData' => [
'id' => 2,
'user_name' => 'john'
]
];
This question already has answers here:
Push to array reference
(4 answers)
Closed 8 years ago.
In my Perl Programm, I used for testing the following static array definition:
my %data = (
56 => [
{ 'Titel' => 'Test 1',
'Subtitel' => 'Untertest 1',
'Beginn' => '00:05',
'Ende' => '00:50'
},
{ 'Titel' => 'Test 2',
'Subtitel' => 'Untertest 2',
'Beginn' => '00:50',
'Ende' => '01:40'
}
],
58 => [
{ 'Titel' => 'Test 3',
'Subtitel' => 'Untertest 3',
'Beginn' => '00:10',
'Ende' => '01:50'
}
],
51 => [
{ 'Titel' => 'Test 4',
'Subtitel' => 'Untertest 4',
'Beginn' => '00:05',
'Ende' => '00:20'
},
{ 'Titel' => 'Test 5',
'Subtitel' => 'Untertest 5',
'Beginn' => '00:20',
'Ende' => '00:40'
},
{ 'Titel' => 'Test 6',
'Subtitel' => 'Untertest 6',
'Beginn' => '00:40',
'Ende' => '01:05'
}
],
);
Now I like to change it, to get data from a database. My select returns 5 values: an id (like 56, 58 or 51 in my example) and the values for each Titel, Subtitel, Beginn and Ende.
How can I build the same array construct like in my static example?
Thanks in advance! Best regards
Daniel
Assuming you want it at the end, you need to push your hashref into the arrayref stored at $data{$id}:
push #{ $data{$id} }, {
Titel => $titel,
Subtitel => $subtitel,
Beginn => $beginn,
Ende => $ende,
};
Could be something like this. Sorry, it's been a while since I've done perl.
#!/usr/bin/perl
use Data::Dumper;
sub dataset2struc {
my $result = {};
foreach my $row (#_) {
my $id = $row->{'id'};
my $recref = $result->{$id} || ();
my #rec = #{$recref};
delete $row->{'id'};
push(\#rec, $row);
$result->{$id} = \#rec;
}
return $result;
}
my #dataset = ({"id" => 1, "a" => "b"}, {"id" => 2, "a" => "c"}, {"id" => 2, "a" => "d"});
print Dumper(dataset2struc(#dataset));
Is there an easier way to access the value of the 'type' attribute without looping through the whole object to find it?
[
{ type => "voipPassword", vals => ["data"] },
{ type => "sn", vals => ["data"] },
{ type => "voipExtension", vals => [data] },
{ type => "cn", vals => ["data"] },
{ type => "telephoneNumber", vals => [data] },
{ type => "objectClass", vals => ["data"] },
{ type => "phoneMAC", vals => ["data"] },
]
You can access type directly like this example:
#!/usr/bin/perl
use strict;
use warnings;
my $ref = [
{ type => "voipPassword", vals => ["data"] },
{ type => "sn", vals => ["data"] },
{ type => "voipExtension", vals => ["data"] },
{ type => "cn", vals => ["data"] },
{ type => "telephoneNumber", vals => ["data"] },
{ type => "objectClass", vals => ["data"] },
{ type => "phoneMAC", vals => ["data"] },
];
print $ref->[0]->{'type'} . "\n";
print $ref->[1]{'type'} . "\n";
Output:
voipPassword
sn
See perlreftut for more details.
i'm trying to create a test module to test json encoding. i am having issues creating variables that will output correctly with the json encode/decode. if i use just the $cat_1 in the #cats array, it will work fine. however, using both, it prints out "HASH(..." as you can see below.
use strict;
use JSON;
use Data::Dump qw( dump );
my $cat_1 = {'name' => 'cat1', 'age' => '6', 'weight' => '10 kilos', 'type' => 'siamese'};
my $cat_2 = {'name' => 'cat2', 'age' => '10', 'weight' => '13 kilos', 'type' => 'siamese'};
my #cats;
push(#cats, $cat_1);
push(#cats, $cat_2);
my $dog_1 = {'name' => 'dog1', 'age' => '7', 'weight' => '20 kilos', 'type' => 'siamese'};
my $dog_2 = {'name' => 'dog2', 'age' => '5', 'weight' => '15 kilos', 'type' => 'siamese'};
my #dogs;
push(#dogs, $dog_1);
push(#dogs, $dog_2);
my $pets = {'cats' => #cats, 'dogs' => #dogs};
my $a = { 'id' => '123', 'name' => 'Joe Smith', 'city' => "Chicago", 'pets' => $pets };
my $json = JSON->new->allow_nonref;
my $encoded = $json->encode($a);
my $decoded = $json->decode( $encoded );
print "\ndump cat_1\n";
dump $cat_1;
print "\ndump cats\n";
dump #cats;
print "\n\nOriginal\n";
dump $a;
print "\n\n";
print "Encoded\n";
print $encoded;
print "\n\n";
print "Decoded\n";
dump $decoded;
print "\n\n";
output
dump cat_1
{ age => 10, name => "cat1", type => "siamese", weight => "10 kilos" }
dump cats
(
{ age => 10, name => "cat1", type => "siamese", weight => "10 kilos" },
{ age => 10, name => "cat2", type => "siamese", weight => "3 kilos" },
)
Original
{
city => "Chicago",
id => 123,
name => "Joe Smith",
pets => {
"cats" => { age => 10, name => "cat1", type => "siamese", weight => "10 kilos" },
"HASH(0x176c3170)" => "dogs",
"HASH(0x1785f2d0)" => { age => 10, name => "dog2", type => "siamese", weight => "3 kilos" },
},
}
Encoded
{"city":"Chicago","pets":{"HASH(0x1785f2d0)":{"weight":"3 kilos","name":"dog2","type":"siamese","age":"10"},"cats":{"weight":"10 kilos","name":"cat1","type":"siamese","age":"10"},"HASH(0x176c3170)":"dogs"},"name":"Joe Smith","id":"123"}
Decoded
{
city => "Chicago",
id => 123,
name => "Joe Smith",
pets => {
"cats" => { age => 10, name => "cat1", type => "siamese", weight => "10 kilos" },
"HASH(0x176c3170)" => "dogs",
"HASH(0x1785f2d0)" => { age => 10, name => "dog2", type => "siamese", weight => "3 kilos" },
},
}
This line
my $pets = {'cats' => #cats, 'dogs' => #dogs};
is a red flag. It's valid Perl, but it's not doing what you would expect. Perl will flatten your lists in this construction, so if #cats contains ($cat_1,$cat_2) and #dogs containts ($dog_1,$dog_2), your expression is parsed as
my $pets = { 'cats', $cat_1, $cat_2, 'dogs', $dog_1, $dog_2 };
which is like
my $pets = { 'cats' => $cat_1, $cat_2 => 'dogs', $dog_1 => $dog_2 }
with the hash references $cat_2 and $dog_1 getting stringified before being used as hash keys.
Hash values must be scalar values, not arrays. But array references are OK. Try:
my $pets = {'cats' => \#cats, 'dogs' => \#dogs};
The problem is in the creation of $pets:
my $pets = {'cats' => #cats, 'dogs' => #dogs};
Is roughly equivalent to:
my $pets = {'cats', {name => 'cat1', ...}, {name => 'cat2', ...},
'dogs', {name => 'dog1', ...}, {name => 'dog2, ...} };
Which is the same as:
my $pets = {
'cats' => {name => 'cat1', ...},
{name => 'cat2'}, => 'dogs',
{name => 'dog1', ...}, => {name => 'dog2}
};
You want to use ArrayRefs:
my $pets = {'cats' => \#cats, 'dogs' => \#dogs};
Which is:
my $pets = {
'cats' => [
{name => 'cat1', ...},
{name => 'cat2', ...},
],
'dogs' => [
{name => 'dog1', ...},
{name => 'dog2', ...},
],
};
Which is also how you could declare the whole data structure at once.