Joining arrays using references when defining a new data structure - arrays

First post here for me :)
I'm not sure if this is even possible, but I would like to find out.
Given the following code...
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %email_addresses = (
'fred' => [
'"Fred Blogs" <fred.blogs#domain.com>',
'"Fred Blogs" <fred.blogs#hotmail.com>'
],
'jane' => [
'"Jane Smith" <jane.smith#domain.com>',
'"Jane Smith" <jane.smith#hotmail.com>',
'"Jane Smith" <jane.smith#somwhere.com>'
],
'tom' => [
'"Tom Jones" <tom.jones#domain.com>'
]
);
my %recipients = (
'success' => [
$email_addresses{'fred'},
$email_addresses{'jane'}
],
'failure' => [
$email_addresses{'tom'}
]
);
print Data::Dumper->Dump([\%recipients], ['recipients']);
The output is
$recipients = {
'success' => [
[
'"Fred Blogs" <fred.blogs#domain.com>',
'"Fred Blogs" <fred#hotmail.com>'
],
[
'"Jane Smith" <jane.smith#domain.com>',
'"Jane Smith" <jane.smith#hotmail.com>',
'"Jane Smith" <jane.smith#somwhere.com>'
]
],
'failure' => [
[
'"Tom Jones" <tom.jones#domain.com>'
]
]
};
The result is that both $recipients{'success'} and $recipients{'failure'} are two dimensional arrays, however this is not what I want. I would like them to be one dimensional arrays.
That is, for $recipients{'success'}, I want the list of Jane's email addresses to be appended to the list of Fred's email addresses resulting in a one dimensional array containing 5 elements. Similarly, $recipients{'failure'} would be a one dimensional array containing only 1 element.
So, I want it to look like this...
$recipients = {
'success' => [
'"Fred Blogs" <fred.blogs#domain.com>',
'"Fred Blogs" <fred#hotmail.com>',
'"Jane Smith" <jane.smith#domain.com>',
'"Jane Smith" <jane.smith#hotmail.com>',
'"Jane Smith" <jane.smith#somwhere.com>'
],
'failure' => [
'"Tom Jones" <tom.jones#domain.com>'
]
};
Now here's the catch... I want to know if this can be done at the point where I define my %recipients - all in one statement.
I know I can achieve what I want programmatically after the definition using additional statements, but I'm curious to know if it can be done all in one. I've tried various combinations of (), [], {} and dereferencing but nothing has worked.
Thanks all.

You're essentially wanting to flatten a list of array references. Yes, that is easily accomplished.
I would advise that you just use map and pass a list of keys that you want to translate like so:
use strict;
use warnings;
use Data::Dumper;
my %email_addresses = (
'fred' => [
'"Fred Blogs" <fred.blogs#domain.com>',
'"Fred Blogs" <fred.blogs#hotmail.com>',
],
'jane' => [
'"Jane Smith" <jane.smith#domain.com>',
'"Jane Smith" <jane.smith#hotmail.com>',
'"Jane Smith" <jane.smith#somwhere.com>',
],
'tom' => [
'"Tom Jones" <tom.jones#domain.com>',
]
);
my %recipients = (
'success' => [map #{$email_addresses{$_}}, qw(fred jane)],
'failure' => [map #{$email_addresses{$_}}, qw(tom)],
);
print Data::Dumper->Dump([\%recipients], ['recipients']);
Outputs:
$recipients = {
'success' => [
'"Fred Blogs" <fred.blogs#domain.com>',
'"Fred Blogs" <fred.blogs#hotmail.com>',
'"Jane Smith" <jane.smith#domain.com>',
'"Jane Smith" <jane.smith#hotmail.com>',
'"Jane Smith" <jane.smith#somwhere.com>'
],
'failure' => [
'"Tom Jones" <tom.jones#domain.com>'
]
};

Related

Convert the following array data as Json in react

I need to convert the following array data to Json in react.
I tried map method, but it is not the correct way. I need key value pair, so that i can pass it to server as json
[
[
"channel",
"s1"
],
[
"category",
"Account 1"
],
[
"accountAdministration",
"Partnership 1"
],
[
"partnershipAccounting",
"1 level Performance issues"
],
[
"requestCategory",
"Research"
],
[
"severity",
"Blocker"
],
[
"activationDate",
"2020-10-29T05:54:00.000Z"
],
[
"managerApproved",
true
]
]
Try using reduce and creating an object:
var arr = []; // your array
var data = arr.reduce((map, item) => {
map[item[0]] = item[1];
return map;
}, {});
The data object will be in the following format:
{
"accountAdministration": "Partnership 1",
"activationDate": "2020-10-29T05:54:00.000Z",
"category": "Account 1",
"channel": "s1",
...
}
Worked with
Object.fromEntries([
[
"channel",
"s1"
],
[
"category",
"Account 1"
],
[
"accountAdministration",
"Partnership 1"
],
[
"partnershipAccounting",
"1 level Performance issues"
],
[
"requestCategory",
"Research"
],
[
"severity",
"Blocker"
],
[
"activationDate",
"2020-10-29T05:54:00.000Z"
],
[
"managerApproved",
true
]
])

Laravel how to return object into JSON Arrays data type without keys

I have DB query in the Controller like this:
$query = User::all('id','name','email')->take(2);
$users = ["data" => $query];
return $users;
And get the result:
{
"data": [
{
"id": 1,
"name": "Peter",
"email": "peter#peter.com"
},
{
"id": 2,
"name": "John",
"email": "john#john.com"
}
]
}
But i'm expecting to get JSON Arrays data type without keys like this:
{
"data": [
[
"1",
"Peter",
"peter#peter.com"
],
[
"2",
"John",
"john#john.com"
]
]
}
I need to get this type for DataTables JSONP data source for remote domains.
https://datatables.net/examples/server_side/jsonp.html
How to do this?
Thanks.
You can try array_values like so:
$query = User::all('id', 'name', 'email')->take(2);
$users = ["data" => $query->map(function (User $user) {
return array_values($user->attributesToArray());
})];
return $users;
How about something like this?
$query = User::all('id', 'name', 'email')->take(2);
$userValues = $query->map(function($item, $key) {
return [
$item['id'],
$item['name'],
$item['email']
];
})
$users = ["data" => $userValues];
return $users;
// result
{
"data": [
[
1,
"Amy Wilderman",
"vrau#example.net",
],
[
2,
"Bria Lindgren PhD",
"trevor.armstrong#example.org",
],
]
}

Get the value of 2nd index by mapping 1st index through Hash of Arrays in Perl

I have a Perl file code state.pl where I am trying to retrieve full name of State based on State code from Hash of Arrays. Following is the code:
my $all_state_details = {
IN => [
[
"AP",
"Andhra Pradesh"
],
[
"AN",
"Andaman and Nicobar Islands"
],
[
"AR",
"Arunachal Pradesh"
],
],
US => [
[
"AL",
"Alabama"
],
[
"AK",
"Alaska"
],
[
"AS",
"American Samoa"
],
],
};
my $state = 'AN';
my $country = 'IN';
my #states = $all_state_details->{$country};
my #state_name = grep { $_->[0] eq $state } #states;
print #state_name;
When I run the script, I get the blank output
I want the output as just:
Andaman and Nicobar Islands
The #{ ... } dereference operation is necessary to convert the array reference in $all_state_details->{$country} into an array suitable for use with grep.
print map { $_->[1] }
grep { $_->[0] eq $state } #{$all_state_details->{$country}};
The right way to do this kind of lookup is with a hash of hashes.
e.g.
%all_state_details = (
IN => {
"AP" => "Andhra Pradesh",
"AN" => "Andaman and Nicobar Islands",
},
);
Then you just do a direct lookup.
print $all_state_details{IN}{AN};
Read https://perldoc.perl.org/perldsc.html#HASHES-OF-HASHES
HTH

Can I send 2 dimension array as parameter in POSTMAN?

I need to send parameters as array of objects in POSTMAN.
"array": [
{"field1": "html", "field2": "5"},
{"field1": "css", "field2": "3"}
]
I know that array must send as array[] but how can I set one item of the array as an object?
I tried this
"array[]" : "{"field1": "html", "field2": "5"}"
But I'm getting a 500 response error.
Just send it in raw format(in json) and specify data type as application/json. Worked for me
If array of arrays works for you as well, you can send them like this:
Go to bulk edit
userdata:[{"name":"name1","email":"email1#gmail.com"},{"name":"name2","email":"email2#gmail.com"},{"name":"name3","email":"email3#gmail.com"}]
I found the solution adding raw data as JSON.
from body > raw
[
{
"text": "Buy fish",
"completed": true
},
{
"text": "Go for walk check",
"completed": false
},
{
"text": "Pick baby from school",
"completed": false
}
]
I know this ia an old post but this might help someone out there.... it's what worked for me:
In your controller: (Laravel/PHP)
$validator = Validator::make($request->all(), [
'fields' => 'nullable|array|min:1',
'fields.key1.*' => 'integer',
'fields.key2.*' => 'integer'
]);
if ($validator->fails())
{
return ...
}
Then in Postman:
fields[0]['key1']
fields[0]['key2']
.... and you can repeat this as many times as you need to, just increment the index
fields[1]['key1']
fields[1]['key2']
If you dd(fields) in your controller, you'd get:
0 => [
key1 => value1
key2 => value2
]
1 => [
key1 => value3
key2 => value4
]
.....

perl array of hashes using Tie::IxHash

I am trying to create an array of hashes with each has being an tied, ordered IxHash. When looping through my initial hash, the keys are indeed in order. However, as soon as I push them onto an array, the ordering disappears. I know this is my poor knowledge of what is happening with the hash when it is pushed on the array, but if somebody could enlighten me, it would be much appreciated.
#! /usr/bin/perl -w
use strict;
use Data::Dumper;
use Tie::IxHash;
my #portinfo;
tie (my %portconfig, 'Tie::IxHash',
'name' => [ 'Name', 'whatever' ],
'port' => [ 'Port', '12345' ],
'secure' => [ 'Secure', 'N' ]
);
print "Dump of hash\n";
print Dumper(%portconfig);
print "\nDump of array\n";
push #portinfo, {%portconfig};
print Dumper(#portinfo);
The output of this :-
Dump of hash
$VAR1 = 'name';
$VAR2 = [
'Name',
'whatever'
];
$VAR3 = 'port';
$VAR4 = [
'Port',
'12345'
];
$VAR5 = 'secure';
$VAR6 = [
'Secure',
'N'
];
Dump of array
$VAR1 = {
'secure' => [
'Secure',
'N'
],
'name' => [
'Name',
'whatever'
],
'port' => [
'Port',
'12345'
]
};
Your code:
push #portinfo, {%portconfig};
print Dumper(#portinfo);
takes the tied hash %portconfig and places its contents into a new anonymous hash which is then pushed into #portinfo. Thus, you have an anonymous, non-ordered hash in your array.
What you probably mean to do is
push #portinfo, \%portconfig;
print Dumper(#portinfo);
This pushes a reference to %portconfig into #portinfo, thereby retaining your required ordering.
Thus:
#! /usr/bin/perl -w
use strict;
use Data::Dumper;
use Tie::IxHash;
my #portinfo;
tie (my %portconfig, 'Tie::IxHash',
'name' => [ 'Name', 'whatever' ],
'port' => [ 'Port', '12345' ],
'secure' => [ 'Secure', 'N' ]
);
print "Dump of hash\n";
print Dumper(%portconfig);
print "\nDump of array\n";
push #portinfo, \%portconfig;
print Dumper(#portinfo);
Gives
C:\demos>perl demo.pl
Dump of hash
$VAR1 = 'name';
$VAR2 = [
'Name',
'whatever'
];
$VAR3 = 'port';
$VAR4 = [
'Port',
'12345'
];
$VAR5 = 'secure';
$VAR6 = [
'Secure',
'N'
];
Dump of array
$VAR1 = {
'name' => [
'Name',
'whatever'
],
'port' => [
'Port',
'12345'
],
'secure' => [
'Secure',
'N'
]
};

Resources