Perl: Counting elements in a complex data structure - arrays

I am new to complex data structures. I kind of understand the idea behind them but am having some difficulty getting the data out. I found out the structure of my current problem child by using Data::Dumper....
$VAR1 = {
'4' => {
'engine_coded' => 0,
'name' => 'FILTER_1',
'filter_actions' => {
'X_Override_Queue_Level' => 'Value'
},
'filter_criteria' => [
[
'X_Charge',
'=',
'X_CHARGE_1'
]
]
}
};
What I am needing to do is make sure that given a filter name ("4" in this case") that "name" has a value, as well as "filter_actions" and "filter_criteria".
Anyone have an idea how to best accomplish this? Many thanks!
Janie

Let's break this down into pieces...
First, create a function which validates a structure:
sub validate
{
my ($data) = #_;
# always return true for now
return 1;
}
Now let's start filling in the bits... you want to use a filter name as part of the validation checks, so let's add that as an argument:
sub validate
{
my ($data, $filter_name) = #_;
# always return true for now
return 1;
}
Before doing anything else, it would make sense to check if that filter name exists as a key; if it doesn't, validation has failed:
sub validate
{
my ($data, $filter_name) = #_;
return if not exists $data->{$filter_name};
# otherwise, return true
return 1;
}
Now also check that there is a value. Since definedness in a hash key is a superset of 'exists' (any value that is defined must also exist, but not every value that exists needs to be defined - as undef could be the value), the first check can be omitted:
sub validate
{
my ($data, $filter_name) = #_;
return if not defined $data->{$filter_name};
# otherwise, return true
return 1;
}
We've checked that the filter_name key is present in the data and it is defined, but before looking one level deeper, we need to confirm that it really is a hashref:
sub validate
{
my ($data, $filter_name) = #_;
return if not defined $data->{$filter_name};
return if ref $data->{$filter_name} ne 'HASH';
# otherwise, return true
return 1;
}
Now look for the 'filter_actions' and 'filter_criteria' keys under the filter name:
sub validate
{
my ($data, $filter_name) = #_;
return if not defined $data->{$filter_name};
return if ref $data->{$filter_name} ne 'HASH';
return if not defined $data->{$filter_name}{filter_actions};
return if not defined $data->{$filter_name}{filter_actions};
# otherwise, return true
return 1;
}
That's it! Be sure to read up on using perl data structures in perldoc perlreftoot, perldoc perlref, and perldoc perldsc.

You can access filter_actions/etc by checking $var->{4}->{filter_actions}. You might look at perldsc for a comprehensive overview of perl's data structures.

Here's my take. You are just checking to see if the data is there in the filter. If you wanted more structural validation that part comes later.
use List::Util qw<first>;
sub validate_filter {
my ( $filters_ref, $filter_name ) = #_;
my $filter = $filter_name ? $filters_ref->{$filter_name} : $filters_ref;
return 1 unless
my $missing
= first { !!$filter->{ $_ } }
qw<name filter_actions filter_criteria>
;
if ( $missing ) {
Carp::croak( '"Missing '$missing' in filter!" );
}
}
Okay, my first thought was that it would accept the structure and the name, but of course is you know enough when you call
validate_filter( $filters, 4 );
you know enough to pass:
validate_filter( $filters->{4} );
So the parameter processing might not be the easiest to understand, but it makes sense in terms of parameter passing.
If you're after validating structure, you might choose this route. Based on your data, I show an example of a validation failure if a given filter_criteria cluster does not have a '=' operator every 3rd slot.
Like so:
use Carp qw<croak>;
use List::Util qw<first>;
use Params::Util ();
sub _test {
return 1 if shift->( $_ );
local $Carp::CarpLevel = $Carp::CarpLevel + 2;
Carp::croak( shift );
}
my $validators
= { filter_actions => sub {
croak 'filter_actions is not deinfed!' unless defined;
_test( \&Params::Util::_HASH, 'filter_actions must be hash!' );
}
, filter_criters => sub {
croak 'filter_criteria is not defined!' unless defined $crit;
_test( \&Params::Util::_ARRAY, 'filter_criteria must be non-empty ARRAY!' );
foreach ( #$crit ) {
_test( \&Params::Util::_ARRAY, 'criteria must be non-empty ARRAYs' );
_test( sub {
my $arr = shift;
return if #$arr % 3;
# return whether any slot in sequence is not '='
return !first { $arr->[$_] ne '=' }
# every 3 beginning at 1
grep { $_ % 3 == 1 } (1..$#$arr)
;
}
, 'criteria must be key-value pairs separated by equal sign!'
);
}
}
};
And this would change the validate_filter sub like so:
sub validate_filter {
my ( $filters_ref, $filter_name ) = #_;
my $filter = $filter_name ? $filters_ref->{$filter_name} : $filters_ref;
return 1 unless
my $missing
= first {
return 1 unless $filter->{ $_ };
return unless my $validator = $validators->{ $_ };
local $_ = $filter->{ $_ };
return 1 if $validator->( $_ );
}
qw<name filter_actions filter_criteria>
;
if ( $missing ) {
Carp::croak( "Missing '$missing' in filter!" );
}
}

Related

Using List::Util first on an array of Moose objects

I have an array called aTestCaseList which is initialized and filled with (Moose) objects of type "Testcase". As expected I can print out the Attribute TestName of every Testcase Object in aTestCaseList. But when I try to find the first Object in the list named "Test4" using https://perldoc.perl.org/List/Util.html#first I get the following error
Can't call method "TestName" on an undefined value
Why are the objects in the array suddenly undefined?
use Testcase;
my #aTestcaseList=();
for (my $i=1; $i <= 9; $i++) {
push(#aTestcaseList,Testcase->new("Test".$i));
}
my $sTestcase="Test4";
foreach my $sTestDummy(#aTestcaseList)
{
#Works as expected and prints: Test1 Test2 Test3 ... Test9
print $sTestDummy->TestName." ";
}
# throws the error:
my $sFindTest=first {$_->TestName eq $sTestcase} #aTestcaseList;
package Testcase;
use Moose;
use namespace::autoclean;
has 'TestName' => (is =>'ro',isa=>'Str');
around BUILDARGS => sub
{
my $orig = shift;
my $class = shift;
if ( #_ == 1 && ! ref $_[0] ) {
return $class->$orig(TestName => $_[0]);
}
else {
return $class->$orig(#_);
}
};
__PACKAGE__->meta->make_immutable;
1;
You forgot to import the function first from List::Util like
use List::Util qw(first);

NOT an array reference error in Perl

I'm getting an error in Perl and I can't work out why.
Error: Not an ARRAY reference at Service.pm, line 20
my $array = [ { name => 'George', surname => 'Marley' } ];
my $helper = CustMessage->new();
$helper = CustMessage->getMessage($array);
then in my utility file I have:
sub getMessage {
my ($self, $args) = #_;
my $stringsArray = shift;
my $strings = Service->new({
serviceId => $self->_serviceId(),
});
return unless $strings;
$strings->getStrings($stringsArray);
}
and then in the Service method is:
sub getStrings {
my ($stringsArray, $self) = shift;
my #keys = map({ $_->{'name'} } #{$stringsArray});
my $key = join('', #keys);
$key = MIME::Base64::encode($key);
my %results;
$results{$key} = $self->_callStrings->($stringsArray);
$results{$key}->initialize();
$results{$key} = $self->{serviceCalls}->{getStrings};
return $self->{serviceCalls}->{getStrings};
}
The error is on line 2 of the getStrings method in Service.pm:
my #keys = map({ $_->{'name'} } #{$stringsArray});
The lines
my $helper = CustMessage->new();
$helper = CustMessage->getMessage($array);
are very odd. You are creating a new CustMessage object in $helper and immediately discarding it and overwriting it with the result of
CustMessage->getMessage($array);
which, apart from any inheritance that may be going on, is identical to
getMessage('CustMessage', $array);
and I am suspecting that you don't have a real class defined as you call it your "utility file"
Because getMessage receives its arguments like this
my ($self, $args) = #_;
my $stringsArray = shift;
you are left with
($self, $args) = ('CustMessage', $array)
$stringsArray = 'CustMessage'
and you never use $args again so your array reference is lost.
I cannot be sure what it is you actually want, because, as I said, I suspect that you don't have a proper CustMessage.pm file. But you could try
my $helper = CustMessage->new;
my $message = $helper->getMessage($array);
and then
sub getMessage {
my ($self, $stringsArray) = #_;
...
}
RE: I'm getting an error in Perl and I can't work out why. Error: Not an ARRAY reference at Service.pm, line 20
Try
my ($self, $stringsArray) = #_;
instead of
my ($stringsArray, $self) = shift;
# $self is always undef here due one element list assignment
since getStrings() is object method and object instance is always first element in #_ array.

Passing an array into Perl subroutine

I have a subroutine that should take an array as input, make it into a CSV, and POST it to a URL. Here's what I've got so far:
An example array:
[ 2823383062, 1411691539, 1411691541, 'outgoing',
'SIP/fr', 'user#2000', '2000', 'SIP/2000-000000a2',
undef, '6125551234', 'SIP/fr-000000a3', undef,
undef, 8, 'Answered', 2,
1, 'nada'
];
The subroutine:
sub send_http {
my #http = #_;
my $h = LWP::UserAgent->new;
$h->timeout(15);
$h->agent(undef);
my $testkey = "1234";
my $apikey = "4567";
my $posting;
foreach my $v ( \#http ) {
if ( defined $v ) {
$posting = join( ',', $posting, $v );
} else {
$posting = join( ',', $posting, "" );
}
}
my $api_response = $h->post( 'http://url.com/v1/post.cfm',
[ key => $testkey, method => 'pushCalls', rawdata => $posting ] );
}
Forgive all the horrible things I've done; this is my first time using Perl and I'm still learning all sorts of stuff. My issue is that I can't seem to get the values from the array I pass into it past the first array variable declaration (#http). I've read something about getting a reference of the array, but am not sure where/how to do so. Any help is appreciated.
Edit:
Here's the entire script. It does (or should) two things; send some string of data to a TCP socket, and some other data gets POST'ed to a URL. Thanks everyone for your help.
#!/usr/bin/perl
use EV;
use Asterisk::AMI;
use Net::Telnet;
use HTTP::Request::Common;
use LWP::UserAgent;
use strict;
use warnings;
use Data::Dumper;
my %call;
my $t = new Net::Telnet (
Timeout => 10,
Port => '1234',
Telnetmode => 1
);
my $astman = Asterisk::AMI->new(PeerAddr => '127.0.0.1',
PeerPort => '5038',
Username => 'secret',
Secret => 'user',
Events => 'on',
Handlers => {
# default => \&eventhandler,
Dial => \&dialcheck,
Bridge => \&bridgecheck,
Newchannel => \&newchannel,
Newexten => \&newexten,
Hangup => \&hangup,
Newstate => \&outring
}
);
die "Unable to connect to asterisk" unless ($astman);
sub send_pos {
my ($pos_string,$telnet) = #_;
$telnet->open('127.0.0.1');
printf $t $pos_string;
$telnet->close()
}
sub send_http {
my $http = shift; ##_;
my $h = LWP::UserAgent->new;
$h->timeout(15);
$h->agent(undef);
my $testkey = "1234";
my $apikey = "5678";
my $posting;
foreach my $v ( #http ) {
if ( defined $v ) {
$posting = join(',', $posting,$v);
} else {
$posting = join(',', $posting,"");
}
}
my $api_response = $h->post( 'http://url.com/v1/post.cfm',[key => $testkey,method => 'pushCalls',rawdata => $posting]);
}
sub eventhandler {
# Default event handler, not used
my ($ami, $event) = #_;
print 'Got Event: ',$event->{'Event'},"\r\n";
}
sub newchannel {
my ($ami, $event) = #_;
my $unique_id = $event->{'Uniqueid'};
if ( not exists $call{$unique_id} ) {
my $this_call = $call{$unique_id};
if ( (not defined $this_call->{'gravityfree'}[3]) ) {# || ($this_call->{'gravityfree'}[3] !~ /incoming|outgoing/) ) {
if ( $event->{'Context'} =~ /from-trunk/ ) {
# Call is inbound
$this_call->{'caller_name'} = $event->{'CallerIDName'};
$this_call->{'caller_number'} = substr $event->{'CallerIDNum'}, -10;
$this_call->{'dnis'} = substr $event->{'Exten'}, -10;
$this_call->{'status'} = "remote";
$this_call->{'holdstart'} = time();
# Data required for Gravity Free
$this_call->{'gravityfree'}[0] = int($event->{'Uniqueid'})+int(time());
$this_call->{'gravityfree'}[3] = "incoming";
$this_call->{'gravityfree'}[5] = $event->{'CallerIDName'};
$this_call->{'gravityfree'}[6] = substr $event->{'CallerIDNum'}, -10;
$this_call->{'gravityfree'}[7] = $event->{'Channel'};
$this_call->{'gravityfree'}[11] = substr $event->{'Exten'}, -10;
# Can't remember why this is here:
$call{$unique_id} = $this_call;
} elsif ( $event->{'Context'} =~ /from-internal/ ) {
# Call is outbound
# Separate from calls to stations
if( length($event->{'CallerIDNum'}) < length($event->{'Exten'}) ) {
$this_call->{'status'} = "remote";
# Data required for Gravity Free
$this_call->{'gravityfree'}[0] = int($event->{'Uniqueid'})+int(time());
$this_call->{'gravityfree'}[9] = substr $event->{'Exten'}, -10;
$this_call->{'gravityfree'}[3] = "outgoing";
$this_call->{'gravityfree'}[6] = $event->{'CallerIDNum'};
$this_call->{'gravityfree'}[5] = $event->{'CallerIDName'};
$call{$unique_id} = $this_call;
} elsif ( length($event->{'CallerIDNum'}) == length($event->{'Exten'}) ) {
# Call is station to station
$this_call->{'status'} = "station-to-station";
}
}
}
}
}
sub newexten {
my ($ami, $event) = #_;
my $unique_id = $event->{'Uniqueid'};
my $this_call = $call{$unique_id};
# Handles inbound calls only
if ( defined $this_call->{'status'} && $this_call->{'status'} ne "station-to-station" ) {
# Call is not station to station
# Check if the DID has been defined
if ( not defined $this_call->{'gravityfree'}[13] ) {
if ( $event->{'Context'} eq 'ext-group' ) {
# Data required for Gravity Free
$this_call->{'gravityfree'}[13] = $event->{'Extension'};
}
}
}
}
sub dialcheck {
my ($ami, $event) = #_;
my $unique_id = $event->{UniqueID};
if ( exists $call{$unique_id} ) {
my $this_call = $call{$unique_id};
if ( defined $this_call->{'status'} && $this_call->{'status'} ne "station-to-station" ) {
# Call is not station to station
if ( $event->{'SubEvent'} eq 'Begin' && $this_call->{'gravityfree'}[3] =~ "incoming" ) {
# Call is inbound
$this_call->{'system_extension'} = $event->{'Dialstring'};
$this_call->{'dest_uniqueid'} = $event->{'DestUniqueID'};
# Data required for Gravity Free
$this_call->{'gravityfree'}[4] = $1 if $event->{'Channel'} =~ /(.+(?=\-\w+)).*/;
# Telnet data to Prodigy
my $sending = "R|$this_call->{'caller_name'}|$this_call->{'caller_number'}|$this_call->{'system_extension'}||$this_call->{'dnis'}|";
send_pos($sending,$t);
$this_call->{'status'} = "ringing";
} elsif ( $event->{SubEvent} eq 'Begin' && $this_call->{'gravityfree'}[3] =~ "outgoing" ) {
# Call is outbound
# Data required for Gravity Free
$this_call->{'gravityfree'}[4] = $1 if $event->{'Destination'} =~ /(.+(?=\-\w+)).*/;
$this_call->{'gravityfree'}[10] = $event->{'Destination'};
$this_call->{'gravityfree'}[7] = $event->{'Channel'};
}
}
}
}
sub outring {
my ($ami, $event) = #_;
my $unique_id = $event->{'Uniqueid'};
my $this_call = $call{$unique_id};
if ( defined $this_call->{'status'} && $this_call->{'status'} ne "station-to-station" ) {
# Call is not station to station
if ( not defined $this_call->{'holdstart'} && $this_call->{'gravityfree'}[3] eq "outgoing" ) {
# Call is outbound
$this_call->{'holdstart'} = time();
}
}
}
sub bridgecheck {
my ($ami, $event) = #_;
my $unique_id = $event->{'Uniqueid1'};
my $this_call = $call{$unique_id};
if ( defined $this_call->{'status'} && $this_call->{'status'} ne "station-to-station" ) {
# Call is not station to station
if ( $event->{'Bridgestate'} eq "Link" && length($event->{'CallerID2'}) <= 4 ) {
# Call is inbound
$this_call->{'dest_uniqueid'} = $event->{Uniqueid2};
# Data required for Gravity Free
$this_call->{'gravityfree'}[1] = time();
$this_call->{'gravityfree'}[10] = $event->{Channel2};
my $sending = "A|$this_call->{caller_name}|$this_call->{caller_number}|$event->{CallerID2}||$this_call->{dnis}|";
send_pos($sending,$t);
$this_call->{'status'} = "answered";
} elsif ( $event->{'Bridgestate'} eq "Link" && length($event->{'CallerID2'}) >= 4 ) {
# Call is outbound
$this_call->{'gravityfree'}[1] = time();
$this_call->{'gravityfree'}[13] = $this_call->{'gravityfree'}[1]-$this_call->{holdstart};
}
}
}
sub hangup {
my ($ami, $event) = #_;
my $unique_id = $event->{'Uniqueid'};
my $this_call = $call{$unique_id};
if ( defined $this_call->{'status'} && not defined $this_call->{'gravityfree'}[16] && $this_call->{'status'} ne "station-to-station" ) {
# Call is not station to station
if ( $event->{'Cause-txt'} eq "Normal Clearing" ) {
# Call was hungup normally
$this_call->{'dest_uniqueid'} = $event->{Uniqueid};
# Call has ended, get date/time
$this_call->{'gravityfree'}[2] = time();
# Mark call 'completed'
$this_call->{'gravityfree'}[16] = 1;
# Set notes to nothing
$this_call->{'gravityfree'}[17] = 'nada';
if ( defined $this_call->{'gravityfree'}[3] && $this_call->{'gravityfree'}[3] eq "incoming") {
# Call was inbound
if ( defined $this_call->{'gravityfree'}[1] ) {
# Call was answered
$this_call->{'gravityfree'}[13] = $this_call->{'gravityfree'}[1]-$this_call->{holdstart};
$this_call->{'gravityfree'}[14] = "Answered";
$this_call->{'gravityfree'}[15] = $this_call->{'gravityfree'}[2]-$this_call->{'gravityfree'}[1];
$this_call->{'gravityfree'}[8] = $event->{'ConnectedLineName'};
$this_call->{'gravityfree'}[9] = substr $event->{'ConnectedLineNum'}, -10;
# POST data to gravity free
send_http(\$this_call->{'gravityfree'});
} else {
# Call was abandoned
$this_call->{'gravityfree'}[14] = "Abandoned";
$this_call->{'gravityfree'}[13] = $this_call->{'gravityfree'}[2]-$this_call->{holdstart};
$this_call->{'gravityfree'}[15] = 0;
# POST data to gravity free
send_http(\$this_call->{'gravityfree'});
}
} elsif ( defined $this_call->{'gravityfree'}[3] && $this_call->{'gravityfree'}[3] eq "outgoing" ) {
# Call is outbound
if ( defined $this_call->{'gravityfree'}[1] ) {
# Call was bridged at some point
$this_call->{'gravityfree'}[15] = $this_call->{'gravityfree'}[2]-$this_call->{'gravityfree'}[1];
$this_call->{'gravityfree'}[14] = "Answered";
# POST data to gravity free
send_http(\$this_call->{'gravityfree'});
} else {
# Call was hung up before anyone answered
$this_call->{'gravityfree'}[15] = 0;
$this_call->{'gravityfree'}[14] = "Abandoned";
$this_call->{'gravityfree'}[13] = $this_call->{'gravityfree'}[2]-$this_call->{holdstart};
# POST data to gravity free
send_http(\$this_call->{'gravityfree'});
}
}
}
}
}
EV::loop
First question, where are you getting the array you're passing into the subroutine?
I ask because your example array is actually an array reference.
That is:
#array = (1, 2, 3); # This is an array
$ref = [1, 2, 3]; # This is an array reference
If you want to pass an array reference to your subroutine, change the beginning to:
sub send_http {
my $http = shift;
Next, let us consider how to iterate over the elements of an array. This is the proper way to do so:
foreach my $element ( #array ) {
# do stuff ...
}
When you do \# on an array, you are actually creating a reference to the array. Thus, if you truly are passing an array to your subroutine, you should change your loop to the following:
foreach my $v ( #http ) {
However, if you decide to pass your array as a reference, you can dereference the pointer and iterate over its elements like this:
foreach my $v ( #$http ) {
Hope this helps!
EDIT: For the newly uploaded code...
You're very close but we have a couple of small issues:
$this_call->{'gravityfree'} is actually already an array reference, I'm not certain why it's allowing you to address the array elements with $this_call->{'gravityfree'}[INDEX], so perhaps someone more knowledgeable than me can enlighten us all. I will note that the correct way to deference an array is the following:
\#{$this_call->{'gravityfree'}}
Regardless, you can simply pass the reference to your subroutine, no need to create a reference. That is:
send_http($this_call->{'gravityfree'});
Now, inside of your subroutine, you have an array reference. You are correctly reading the subroutine argument, but you need to dereference the reference in your foreach loop. Like so:
foreach my $v ( #$http ) {
# ... loop body
}
Does this make sense? Please let me know if anything is unclear (or not working!)
I'm not entirely sure what you're trying to do but everything depends on how you pass the array to your subroutine. You have two choices, either pass it as an array:
send_http(#array)
or as a reference to an array:
send_http(\#array)
As others have noted, your array is already a reference since you're defining it in square brackets [ ].
Which one you want will depend on what exactly you're doing but the syntax is different. To pass an array and iterate through it:
sub send_http {
my #http = #_;
foreach my $v (#http) {
print "v is $v\n";
}
}
my #aa=("cc","dd");
send_http(#aa);
To pass a reference and iterate through the array it points to:
sub send_http {
## Remove the first value from the #_ array.
my $http = shift #_;
## Dereference it to an array. You could also use #{$http}
foreach my $v (#$http) {
print "v is $v\n";
}
}
my #aa=("cc","dd");
send_http(\#aa);
The main difference is that when you use send_http(\#aa); what you're passing is not an array so you can't treat it as one. It is instead a reference to an array. Something like
send_http(ARRAY(0x1d34030));
So, the contents of #_ are just a single reference, ARRAY(0x1d34030). To treat it as an array, you need to dereference it to get to what it points to.
So, into your send_http, the argument you received is array_ref which is scalar variable, so when you used it you need to dereference to the right type.
NB: square bracket is array_reference
So, please change as below:
my $http = shift;
And please use that as :
foreach my $v ( #$http ) {
Example:
my $array_ref = [1,2,3];
print "Reference: ", $array_ref,"\n";
print "Array: ", #$array_ref,"\n";
Output:
Reference: ARRAY(0x7f8e1c004ee8)
Array: 123

PHP uksort function using global variable fails after PHP upgrade to 5.3.3

I have a user defined sort function which uses a 'global' declaration in order to refer to a multi-dimensional array when deciding on the correct sort order. It used to work fine under PHP 5.1.6 but now fails under 5.3.3.
The code throws PHP warning:
PHP Warning: uksort(): Array was modified by the user comparison function
But the code definitely does not modify the array.
This code duplicates the problem:
$arr = array();
$arr['i1']['val1'] = 99;
$arr['i1']['val2'] = 100;
$arr['i2']['val1'] = 89;
$arr['i2']['val2'] = 101;
function cmp($a, $b)
{
global $arr;
if ($arr[$a]['val2'] > $arr[$b]['val2']) { return 1; }
if ($arr[$a]['val2'] < $arr[$b]['val2']) { return -1; }
return 0;
}
if (uksort($arr, 'cmp'))
{
echo "success";
}
else
{
echo "failure";
}
If you're not going to sort by the actual keys, don't use uksort but usort or uasort:
function cmp($a, $b) {
return $a['val2'] - $b['val2'];
}
uasort($arr, 'cmp');
As sidestepping the issue is so popular, here is the cause of your issue:
global $arr;
I'm sure you sensed that aready, to solve that, remove the line and replace $arr with $GLOBALS['arr']. This removes the modification message and you'er accessing the global variable from the symbol table and not the one uksort is currently operating on.
Example:
<?php
$arr = array();
$arr['i1']['val1'] = 99;
$arr['i1']['val2'] = 100;
$arr['i2']['val1'] = 89;
$arr['i2']['val2'] = 101;
function cmp($a, $b)
{
if ($GLOBALS['arr'][$a]['val2'] > $GLOBALS['arr'][$b]['val2']) { return 1; }
if ($GLOBALS['arr'][$q]['val2'] < $GLOBALS['arr'][$b]['val2']) { return -1; }
return 0;
}
if (uksort($arr, 'cmp'))
{
echo "success\n";
}
else
{
echo "failure\n";
}
print_r($arr);
Output for 5.3.23 - 5.5.3: (other versions crash/misbehave)
success
Array
(
[i1] => Array
(
[val1] => 99
[val2] => 100
)
[i2] => Array
(
[val1] => 89
[val2] => 101
)
)
Ups, maybe that's why you see a warning in more recent versions: (Demo: http://3v4l.org/DkK3v)

Search for hash in an array by value

I have a function which extracts Excel data into an array of hashes like so:
sub set_exceldata {
my $excel_file_or = '.\Excel\ORDERS.csv';
if (-e $excel_file_or) {
open (EXCEL_OR, $excel_file_or) || die("\n can't open $excel_file_or: $!\n");
while () {
chomp;
my ( $id, $date, $product, $batchid, $address, $cost ) = split ",";
my %a = ( id => $id
, date => $date
, product => $product
, batchid => $batchid
, address => $address
, cost => $cost
);
push ( #array_data_or, \%a );
}
close EXCEL_OR;
}
}
Populating the array of hashes is fine. However, the difficult part is searching for a particular item (hash) in the array. I can't seem to locate items that might have an id or 21, or a batchid of 15, or a cost > $20 etc.
How would I go about implementing such a search facility?
Thanks to all,
With the power of grep
my #matching_items = grep {
$_->{id} == 21
} #array_data_or;
If you know there will be only one item returned you can just do this:
my ($item) = grep {
$_->{id} == 21
} #array_data_or;
(Untested, and I haven't written one of these in a while, but this should work)
If you're sure that the search always returns only one occurence or if you're interested in only the first match then you could use the 'first' subroutine found in List::Util
use List::Util;
my %matching_hash = %{ first { $_->{id} == 21 } #array_data_or };
I enclosed the subroutine call in the %{ } block to ensure that the RHS evaluates to a hash.

Resources