Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Count values in multi dimensional array

Reply
Thread Tools

Count values in multi dimensional array

 
 
Oliver Meister
Guest
Posts: n/a
 
      07-26-2006
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;

.....

 
Reply With Quote
 
 
 
 
Oliver Meister
Guest
Posts: n/a
 
      07-26-2006

(E-Mail 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.
>
> > May I ask for a helping hand?

>
> 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

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-26-2006
Oliver Meister <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail 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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-26-2006
Oliver Meister <(E-Mail Removed)> wrote:

> 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...


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Oliver Meister
Guest
Posts: n/a
 
      07-27-2006

[...]

>
> 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].

[...]

>
> Anno


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:umper;


#
# 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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
multi-dimensional arrays to 2-dimensional arrays Wirianto Djunaidi Ruby 2 04-29-2008 07:31 AM
How to restrict multi-dimensional array? opalpa@gmail.com Java 7 02-20-2005 05:25 AM
Multi-dimensional Array in Java? kk_oop Java 1 07-24-2004 11:23 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM



Advertisments