Count values in multi dimensional array

O

Oliver Meister

Dear All
From an XML file, I have an array (hash) generated [1].

Unfortunately I haven't figured out how i can count the number of
values in the array "Exchange".
I was trying with $var = @arr, key and scalar syntax but also my
attempt try was not successful [2].

With this example [1], I am expecting to have a count of 3 for the Key
"Exchange".

May I ask for a helping hand?

Kind Regards
Oliver


[1]

$VAR1 = {
'InputList' => {
'InputListAction' => 'Replace',
'Instrument' => [
{
'Exchange' => 'SWX',
'Identifier' => '.SSHI',
'IdentifierType' =>
'RIC',
'Description' => 'SPI
TOTAL RETURN'
},
{
'Exchange' => 'N/E',
'Identifier' =>
'AUD6MFSR=',
'IdentifierType' =>
'RIC',
'Description' =>
'Australian Dollar 6 month LIBOR'
},
{
'Exchange' => 'N/E',
'Identifier' =>
'AUD7M=',
'IdentifierType' =>
'RIC',
'Description' =>
'Australian Dollar 7 month Forward'
},
{
'Exchange' => 'N/E',
'Identifier' => 'ZAR=',
'IdentifierType' =>
'RIC',
'Description' => 'South
African Rand Spot'
}
],
'Name' => ' TEST Import'
}
};



[2]
.....
$i = 0;
foreach $xChange (
$data->{'InputList'}->{'Instrument'}->['Exchange'] )
{
print $i;

.....
 
O

Oliver Meister

(e-mail address removed)-berlin.de schrieb:


....
How so? The key "Exchange" appears in four hashes. Do you only want
to count the cases where the value is "N/E"? That's the only obvious
way I see to arrive at a count of three.


You'll have to find a way to express your problem more clearly. What
you have written makes no sense.

Anno

Guten Tag Anno

Indeed, I haven't been very clear in my first posting! Reading through
it, makes me think that I can understand it but somebody else. ...At
least, I can understand myself :)... Also the expected count should
be 4 rather than 3.
Sorry for this everybody.

Anyway:
I've wrote a "workarround hack" for my case which counts the entries of
"Exchange":
$i = 0;
while($data->{'InputList'}->{'Instrument'}->[$i]{'Exchange'})
{
$i++;
print "\n i: $i \n";

print $data->{'InputList'}->{'Instrument'}->[$i]{'Exchange'};
}

What I was realy looking for is something like:
foreach ( $data['InputList']['Instrument']['Exchange'] as $var )
and
count( $data['InputList']['Instrument']['Exchange'] );


Certainly my view is limited on HASH and PERL but couldn't find
something working on google as suggested by A. Sinan Unur in the
previous post. What I've found is counting the scalar or the array with
the "usual" methods. This, in my case, wasn't working. I guess it is
because may construct has hash and array mixed (?).

Gruesse
Oliver
 
A

anno4000

Oliver Meister said:
(e-mail address removed)-berlin.de schrieb:
[...]

Anyway:
I've wrote a "workarround hack" for my case which counts the entries of
"Exchange":
$i = 0;
while($data->{'InputList'}->{'Instrument'}->[$i]{'Exchange'})
{
$i++;
print "\n i: $i \n";

print $data->{'InputList'}->{'Instrument'}->[$i]{'Exchange'};
}

That only appears to work because all relevant hashes have the key
"Exchange". That is a poor choice of test data. Add a few that
don't have the key and try again. It will give you randomly wrong
(or sometimes correct) results.
What I was realy looking for is something like:
foreach ( $data['InputList']['Instrument']['Exchange'] as $var )
and
count( $data['InputList']['Instrument']['Exchange'] );

Both the loop and the count function are incorporated in grep().
See "perldoc -f grep" and also see Sinan Unur's suggestion in this
thread.

Anno
 
T

Tad McClellan

Oliver Meister said:
Unfortunately I haven't figured out how i can count the number of
values in the array "Exchange".


There *is no* array "Exchange"!

That is going to make counting things in the array extremely difficult...

With this example [1], I am expecting to have a count of 3 for the Key
"Exchange".


*Why* are you expecting a count of 3?

It looks like there are 4 of them to me...

May I ask for a helping hand?


May we ask for a comprehensible question?

$VAR1 = {
'InputList' => {
'InputListAction' => 'Replace',
'Instrument' => [
{
'Exchange' => 'SWX',


The value associated with key 'Exchange' is a string, not an array.

There is only *one* array in your entire data structure, and it
is associated with the 'Instrument' key, not the 'Exchange' key...
 
O

Oliver Meister

[...]
That only appears to work because all relevant hashes have the key
"Exchange". That is a poor choice of test data. Add a few that
don't have the key and try again. It will give you randomly wrong
(or sometimes correct) results.

Thats true but the key "Exchange" will appear in all data sets anyway.
But it wouldn't be the right way to check the coun't from conceptional
point of view:
Both ways (grep and while loop) are certainly displaying the count but
we count the values in the key and not the number of values in the
forehand array.

For sure, I may haven't pointed that out clearly.

Anyway. I got it working by adopting A. Sinan Unurs suggestion [1].

[...]

I've also posted the test - data [2].

Well, thank you Anno and Sinan Unur for you kind help (and patience ;-)
).

Regards
Oliver,
who is promissing to be more clear on a possible next topic post.



[1] Start copy/ pastable code

#!/usr/local/bin/perl
# Change shebang to your convenience
# Oliver Meister, Example for Posting


#
# Start
#

if (-e "data.xml")
{
print "### [OK] XML File exists !";

&f_initialisation; # Load Class, Module and File

# A. Sinan Unur
my @instruments = @{ $data->{InputList}->{Instrument} };
my $num_ex = grep { $_->{Exchange} } @instruments;
#
my $cnt = @instruments;

print "\nNumber of instruments sets, cnt: $cnt ";
print "\nNumber of instruments sets, num_ex: $num_ex \n";


print "\n\n\n\n more code here";

# [Debug] Display the parsed xml file
print "\n\n ################### \n\n";
print Dumper($data);

}


### FUNCTIONS


#
# Initialize perl mod for XML and Read XML File
#
sub f_initialisation
{
#
# Load Modules
#
use XML::Simple;
use Data::Dumper;


#
# create object
#
my $xml = new XML::Simple;


#
# Read XML File into array $data
#
$data = $xml->XMLin("data.xml");

return $data;
}

# EOF


[2] data.xml

<?xml version="1.0"?>
<ReportRequest xmlns="xxx">
<InputList>
<InputListAction>Replace</InputListAction>
<Name>nnn</Name>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>.SSHI</Identifier>
<Description>SPI TOTAL RETURN</Description>
<Exchange>SWX</Exchange>
</Instrument>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>AUD6MFSR=</Identifier>
<Description>Australian Dollar 6 month LIBOR</Description>
<Exchange>N/E</Exchange>
</Instrument>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>AUD7M=</Identifier>
<Description>Australian Dollar 7 month Forward</Description>
<Exchange>N/E</Exchange>
</Instrument>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>ZAR=</Identifier>
<Description>South African Rand Spot</Description>
<Exchange>N/E</Exchange>
</Instrument>
</InputList>
</ReportRequest>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top