Easiest way to grab "target" key from the hash shown [closed] - arrays

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is the output of Dumper for the hashref in question:
#!/usr/bin/perl
use Data::Dumper;
sub getUpgradeTaskUUIDS {
my $derp = SF::Transaction::Update::getUpgradeTasks();
print "Derp is:\n";
print Dumper $derp;
[... do stuff ...]
}
The following hashref is received by another function. task_readiness may have elements, it may not. I managed to grab the "target" => '_6f5c8c84-6251-11eb-8def-fd627969bf5f_52dcea68-60e9-11eb-830e-f71d7dc3a91f' in some convoluted way, but I am wondering what the cleanest, simplest way would be? I need to be able to grab target -> '' values from both task_readiness and task_upgrade as well if both have applicable elements:
Derp is:
$VAR1 = {
'tasks_readiness' => [],
'tasks_upgrade' => [
{
'type' => 6,
'hidden' => 0,
'state_name' => '1',
'allow_html_in_msg' => 1,
'aq_id' => '0e43ed66-663e-11eb-a97b-b72c0f665ee4',
'subtype_name' => '',
'retry_type' => 1,
'safe_to_delete' => 1,
'create_time' => 1612370227,
'delay_run' => 1612370299,
'last_state_change' => 1612370287,
'description' => 'Apply Upgrade 7.0.0-1242 to Devices',
'subtype' => 14,
'group_name' => 'DEVICE_UPGRADE',
'name' => '',
'type_name' => '6',
'retries' => 0,
'state' => 1,
'target' => '_6f5c8c84-6251-11eb-8def-fd627969bf5f_52dcea68-60e9-11eb-830e-f71d7dc3a91f',
'domain' => 'e276abec-e0f2-11e3-8169-6d9ed49b625f',
'cost' => 10,
'pid' => 0,
'message' => '<p><strong>Failed to update 2 devices.<br></strong></p><p><strong>Please reapply policies to your managed devices.</strong></p>',
'user' => 'admin'
}
]
};

The easiest way to get the value you want would probably be:
$derp->{tasks_upgrade}[0]{target}
And increment that 0 if you have other entries in the array.
Note: Thanks to Jim Garrison for pointing out my earlier idiocy.

Related

Get specific key from an array

Here is what I put in my Symfony session
$this->session->set('teams', [
'team_1' => ['MyTeamNameA' => ['player-1' => $safe['team-1-player-1'], 'player-2' => $safe['team-1-player-2'], 'player-3' => $safe['team-1-player-3'], 'player-4' => $safe['team-1-player-4'], 'points' => 0]],
'team_2' => ['MyTeamNameB' => ['player-1' => $safe['team-2-player-1'], 'player-2' => $safe['team-2-player-2'], 'player-3' => $safe['team-2-player-3'], 'player-4' => $safe['team-2-player-4'], 'points' => 0]],
]);
And now, in my Twig, I want to retrieve my team_1's name, for example, I did this:
app.session.get('teams')['team_1']
It doesn't work, but, if I do a dump of this last piece of code, I get this result pictured below:
I feel that i'm close to the answer, and yet so far.
Since keys will return you a simple array, then the keys are integer from 0 to the (array | length) - 1, to express it in Twig.
Note that this is actually the same behaviour as PHP, when you define an array like
['foo', 'bar', 'baz']
that would be strictly equivalent to
[0 => 'foo', 1 => 'bar', 2 => 'baz']
So in your case, since you only have one element in the array you can use array[0] or array.0.
All together, this would work:
{{ (app.session.get('teams').team_1 | keys).0 }}
Could be tested from https://twigfiddle.com/b6iy8i

How can I access an Array within a Hash? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am building an installer for perl modules we require at work. After all of the dependencies are installed, I need to check if the module is working properly "use This::Module". The problem is, all of the modules are being installed within a certain order.
The hash looks like...
my %modules=(
0 => {'name' => '/root/mods/CGI/perl-FCGI-0.74-8.amzn2.0.2.x86_64.rpm', 'method' => 'rpm', 'status'=>'pending'},
1 => {'name' => '/root/mods/CGI/perl-CGI-3.63-4.amzn2.noarch.rpm', 'method' => 'rpm','status'=>'done', 'validate' => ['CGI','CGI::Carp']},
2 => {'name' => '/root/mods/Digest/perl-Digest-1.17-245.amzn2.noarch.rpm', 'method' => 'rpm','status'=>'pending'},
3 => {'name' => '/root/mods/Digest/perl-Digest-MD5-2.52-3.amzn2.0.2.x86_64.rpm', 'method' => 'rpm','status'=>'done', 'validate' => ['Digest::MD5']},
4 => {'name' => '/root/mods/HTTP/perl-Business-ISBN-Data-20120719.001-2.el7.noarch.rpm','method' => 'rpm','status'=>'pending'},
5 => {'name' => '/root/mods/HTTP/perl-Data-Dumper-2.145-3.el7.x86_64.rpm','method' => 'rpm','status'=>'pending'},
6 => {'name' => '/root/mods/HTTP/perl-Business-ISBN-2.06-2.el7.noarch.rpm','method' => 'rpm','status'=>'done'}, 'validate' => ['HTTP::Request::Common']},
Each newline is the start of a new module. Once one the 'status' => 'done' I need to access the modules within 'validate'. This is an array because there are cases where there are multiple modules tied to one install sequence.
How can I loop through and return each array element by itself?
'validate' => {['CGI','CGI::Carp']}},
You don't have arrays.
The value of validate is between { and } so it is a hashref.
The arrayref is the first entry in the hash, so it gets converted to a string to be used as a key.
You end up with something like:
{
'name' => '/root/mods/CGI/perl-CGI-3.63-4.amzn2.noarch.rpm',
'method' => 'rpm',
'status' => 'done',
'validate' => {
'ARRAY(0x7f9ab601c4e0)' => undef
}
};
Make sure you use strict; and use warnings;. It would have alerted you to this:
Odd number of elements in anonymous hash at Untitled.pl line 9.
You need to fix your data structure. If you want an array, then put it in an array.
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use Data::Dumper;
my %modules = (
0 => {
'name' => '/root/mods/CGI/perl-FCGI-0.74-8.amzn2.0.2.x86_64.rpm',
'method' => 'rpm',
'status' => 'pending'
},
1 => {
'name' => '/root/mods/CGI/perl-CGI-3.63-4.amzn2.noarch.rpm',
'method' => 'rpm',
'status' => 'done',
'validate' => [ 'CGI', 'CGI::Carp' ]
},
);
Then you can access the arrayref:
my $arrayref = $modules{1}->{validate};
and loop over it:
foreach my $value (#$arrayref) {
say $value
}

Using the paginator directly is not recognising the configuration

CakePHP Version: 4.0.1
Introduction
I have 2 methods that both use the index view, index and search. On index the column can be selected from a select list and a value can be inputted via an input form control enabling a search by column and value. This data is sent via GET to the search method where empty values are checked and the query is executed and the index view is rendered.
In the later 3x versions with the below configuration the index view had the sort on the selected column which is what it is meant to do.
IE: Index view has due_date sorted on the initial load and I select task_name then submit the form to the search method. The task_name has the sort when the view is rendered.
TASKS CONTROLLER
Public pagination property:
public $paginate = [
'sortWhitelist' => [
'Tasks.due_date',
'Tasks.task_name',
'Tasks.type',
'Tasks.priority',
'Tasks.related_to_name',
'Contacts.first_name',
'Contacts.last_name',
'Accounts.account_name',
'Tasks.task_desc'
]
];
Search Method
I initialise the data received from the index method and apply the config to the pagination property and send the query object to the view.
$this->setPage('');
$this->setSort($this->request->getQuery('column'));
$this->setDirection('asc');
// Validation of the page, sort, direction and limit is done here.
// IE: The $this->getSort() must be a string and not be numeric and has a strlen check
// and the $this->getDirection() can only be a string with values 'asc' or 'desc' etc.
if (!empty($this->getPage())) {
$this->paginate['page'] = $this->getPage();
}
$this->paginate['sort'] = $this->getSort();
$this->paginate['direction'] = $this->getDirection();
$this->paginate['limit'] = $this->getLimit();
debug($this->paginate);
$tasks = $this->paginate($query);
$this->set(compact('tasks'));
The result of debug is:
[
'sortWhitelist' => [
(int) 0 => 'Tasks.due_date',
(int) 1 => 'Tasks.task_name',
(int) 2 => 'Tasks.type',
(int) 3 => 'Tasks.priority',
(int) 4 => 'Tasks.related_to_name',
(int) 5 => 'Contacts.first_name',
(int) 6 => 'Contacts.last_name',
(int) 7 => 'Accounts.account_name',
(int) 8 => 'Tasks.task_desc'
],
'sort' => 'Tasks.task_name',
'direction' => 'asc',
'limit' => (int) 25
]
Result
The sort is on the task_name.
A couple of months ago I upgraded to 4 and have just revisted this functionality to find the sort is on the column that was present on index and not the column that was selected. I tried the below to fix the problem:
I referenced this information in the cookbook. And this from SO.
$config = $this->paginate = [
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
debug($config);
$tasks = $this->Paginator->paginate($query, $config);
debug($this->Paginator);
$this->set(compact('tasks'));
The result of debug $config is:
[
'page' => '',
'sort' => 'Tasks.task_name',
'direction' => 'asc',
'limit' => (int) 25
]
The result of debug $this->Paginator is:
object(Cake\Controller\Component\PaginatorComponent) {
'components' => [],
'implementedEvents' => [],
'_config' => [
'page' => (int) 1,
'limit' => (int) 20,
'maxLimit' => (int) 100,
'whitelist' => [
(int) 0 => 'limit',
(int) 1 => 'sort',
(int) 2 => 'page',
(int) 3 => 'direction'
]
]
}
NOTE: The whitelist contains limit, sort, page and direction? And the limit is 20 and I don't even have a selection of 20?
Result
The sort is on the due_date and I need it on the task_name.
Extra Info
If I then click the sort on task_name the sort is on the task_name. All the sorts work just not on the initial load?
Question
How can I configure the pagination property so the sort is on the task_name from the initial load of the search method.
Thanks Z.
The fix is a bit costly and not ideal but it does work. I do a redirect on the initial load. Basically submit the form to search then redirect back to search. IE:
if ($this->request->getQuery('initial') === 'yes') {
$redirect = $this->request->getQuery('redirect', [
'action' => 'search',
'?' => [
'method' => 'search',
'column' => $this->getColumn(),
'input' => $this->getInput(),
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit(),
'filter' => $this->getFilter(),
]
]);
return $this->redirect($redirect);
exit(0);
}
$config = $this->paginate = [
'sortWhitelist' => [
'Tasks.due_date',
'Tasks.task_name',
'Tasks.type',
'Tasks.priority',
'Tasks.related_to_name',
'Contacts.first_name',
'Contacts.last_name',
'Accounts.account_name',
'Tasks.task_desc'
],
'page' => $this->getPage(),
'sort' => $this->getSort(),
'direction' => $this->getDirection(),
'limit' => $this->getLimit()
];
$tasks = $this->Paginator->paginate($query, $config);
$this->set(compact('tasks'));
The sort is now on the task_name.
This negates the initial load problem and simulates usage after the page initially loads where I know the sorts work.

Laravel 5 form validation of array on different

from request I get an array like this:
'array' => [
0 => ['id' => 1,'val' => 2],
1 => ['id' => 1,'val' => 2]
]
I need to validate it so all ids of array will be unique.
right now I try this validation rule:
'array.*.id' => 'different:array.*.id'
but it will check current array with current array so result will be like
The array.0.id and array.0.id must be different.
You should use distinct rule:
'array.*.id' => 'distinct'

CakePHP 3.x OR In Find Condition With Multiple Same Fields

I would like to query the database for
Plates.page_number => 1 OR Plates.page_number => Cover
I am attempting to use the following code, but I am not getting the results I am looking for because of a duplicate array key, how can I search the same field for two different values?
$query = $this->Ledgers->find('all', array(
'contain' => array(
'Plates' => [
'conditions' => [
'OR' => [
'Plates.plate_title' => 'Front Cover',
'Plates.page_number' => '1',
'Plates.page_number' => 'Cover' // Duplicate Array Key
]
]
], 'Plates.PlateImages', 'Tribes'
),
'conditions' => array(
'Ledgers.disabled' => 'n', 'Ledgers.id IN' => $ledgerIds
)
))->orderAsc('ledger_title');
Please try wrapping your conditions in separate arrays, eg:
'OR' => [
['Plates.page_number' => '1'],
['Plates.page_number' => 'cover'],
...
]
More info can be found in CakePHP docs:
Query Builder -> Advanced Conditions

Resources