Count values in multi dimensional array

Discussion in 'Perl Misc' started by Oliver Meister, Jul 26, 2006.

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

    .....
    Oliver Meister, Jul 26, 2006
    #1
    1. Advertising

  2. -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
    Oliver Meister, Jul 26, 2006
    #2
    1. Advertising

  3. Oliver Meister

    -berlin.de Guest

    Oliver Meister <> wrote in comp.lang.perl.misc:
    > -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
    -berlin.de, Jul 26, 2006
    #3
  4. Oliver Meister <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 27, 2006
    #4
  5. [...]

    >
    > 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::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>
    Oliver Meister, Jul 27, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kk_oop
    Replies:
    1
    Views:
    2,163
  2. Replies:
    7
    Views:
    840
    Ryan Stewart
    Feb 20, 2005
  3. John Harrison
    Replies:
    2
    Views:
    710
    John Harrison
    Jul 14, 2003
  4. Venkat
    Replies:
    4
    Views:
    969
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page