Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts

    Looping through hash??

    Ok I am using JSON correctly and getting a returned key and info. Now I have tried many many different foreach and while loops for hashes and arrays and cannot get my data out. Here is the data that is returned, any ideas on how to itterate through this and get each thing out to print?

    Code:
    [ { 'amount' => '0.83', 'txid' => '8383890f74d3187c3407a0e194754e1ecc291cc693d62aca2a8eff518817e2f3', 'time' => 1317161890, 'account' => '254', 'confirmations' => 1411, 'category' => 'receive', 'address' => '1GvsagzpdF1pF9KGGHSmMM8xFy2a4ZNqP3' }, { 'amount' => '0.57', 'txid' => '714680f5e642917e74f4a893961b5657f7655701a26f9b4cbb251284a50b3f81', 'time' => 1317222804, 'account' => '244', 'confirmations' => 1315, 'category' => 'receive', 'address' => '16LE1RN6zNEroJ9bDMgbNag6Y5TpMK5Gt5' }, { 'txid' => '02ea04b01aeac297b7c46fdf486313a190af0984f8574ef578a03bb9a837ca5a', 'time' => 1317244202, 'fee' => '-0.0005', 'account' => '', 'confirmations' => 1276, 'amount' => '-1.3995', 'address' => '1Du2zR96zyJ7Ln94Myd8tHUckd6xYjE885', 'category' => 'send' }, { 'amount' => 3, 'txid' => '4601aaa87764735a640ac9619505c4d597612c73ff664512d06bf1cf58824f40', 'time' => 1317325740, 'account' => '236', 'confirmations' => 1153, 'category' => 'receive', 'address' => '1NC2xf3Bk26fn178yYLjjq8r4tBKXgyt8r' }, { 'txid' => 'b20da0968d3bd55f6a9d2c4701b1e3f2d15fcd7b3f58bd4145a29b8d012538a9', 'time' => 1317334378, 'fee' => '-0.0005', 'account' => '', 'confirmations' => 1138, 'amount' => '-2.9995', 'address' => '12uSZxgLimvHzh8AYhCyHS3KyCsAkWwUN6', 'category' => 'send' }, { 'amount' => 4, 'txid' => '5377f0a25a414971409755dfa66fc9b29e76f2992f5e651cf89645d806dd79af', 'time' => 1317699190, 'account' => '263', 'confirmations' => 582, 'category' => 'receive', 'address' => '1NZYzyrc5b8HZ91j2LmzUo3fCwvzp9qeJy' }, { 'amount' => '1.26', 'txid' => '8e45fab021d7b14f5e04c15c5531453220bb9408fa0856ecef84700784076948', 'time' => 1317743978, 'account' => '264', 'confirmations' => 523, 'category' => 'receive', 'address' => '1Dd2JRSMf5mz6VafUqZHXJ6B7J99CzttPE' }, { 'amount' => '3.63', 'txid' => 'db1b561e62f53823658178a5b684cb982d4a15e8dc42fb77f22c0310f91de6f4', 'time' => 1317788785, 'account' => '266', 'confirmations' => 460, 'category' => 'receive', 'address' => '1EDuiDj1UhVSpZk1Bgz8Wfxizv5xw2ztBS' }, { 'amount' => '1.32', 'txid' => '788baf497518a11ec043862be9b9daeddf8588e135c87d9eccd1a0bdb1090bba', 'time' => 1317822039, 'account' => '270', 'confirmations' => 409, 'category' => 'receive', 'address' => '16sp6jBmTm7g5NVW6wHAd7zGKhUWmp2hJB' }, { 'txid' => 'f41411a2808f7bea3733478604856c0fa119cab7d0b8468e4d571f12b25975e8', 'time' => 1317822684, 'fee' => '-0.0005', 'account' => '', 'confirmations' => 407, 'amount' => '-10.2095', 'address' => '1BJQvf5YR2cn4bYbuNHXHD4De2cZEDLXc5', 'category' => 'send' } ]
    That is exactly how it's returned to me, I can print this using Dumper, but cannot seem to loop it and get data out. Any thoughts? Thanks!
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    The first step should have been to post the output of Dumper so we could see that structure more clearly.

    What you have is a reference to an array of hash references.

    I used Perl::Tidy to clean up the code and here's one way to loop over it.
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use Data::Dumper;
    
    my $array_ref = (
    [
        {
            'amount' => '0.83',
            'txid' =>
              '8383890f74d3187c3407a0e194754e1ecc291cc693d62aca2a8eff518817e2f3',
            'time'          => 1317161890,
            'account'       => '254',
            'confirmations' => 1411,
            'category'      => 'receive',
            'address'       => '1GvsagzpdF1pF9KGGHSmMM8xFy2a4ZNqP3'
        },
        {
            'amount' => '0.57',
            'txid' =>
              '714680f5e642917e74f4a893961b5657f7655701a26f9b4cbb251284a50b3f81',
            'time'          => 1317222804,
            'account'       => '244',
            'confirmations' => 1315,
            'category'      => 'receive',
            'address'       => '16LE1RN6zNEroJ9bDMgbNag6Y5TpMK5Gt5'
        },
        {
            'txid' =>
              '02ea04b01aeac297b7c46fdf486313a190af0984f8574ef578a03bb9a837ca5a',
            'time'          => 1317244202,
            'fee'           => '-0.0005',
            'account'       => '',
            'confirmations' => 1276,
            'amount'        => '-1.3995',
            'address'       => '1Du2zR96zyJ7Ln94Myd8tHUckd6xYjE885',
            'category'      => 'send'
        },
        {
            'amount' => 3,
            'txid' =>
              '4601aaa87764735a640ac9619505c4d597612c73ff664512d06bf1cf58824f40',
            'time'          => 1317325740,
            'account'       => '236',
            'confirmations' => 1153,
            'category'      => 'receive',
            'address'       => '1NC2xf3Bk26fn178yYLjjq8r4tBKXgyt8r'
        },
        {
            'txid' =>
              'b20da0968d3bd55f6a9d2c4701b1e3f2d15fcd7b3f58bd4145a29b8d012538a9',
            'time'          => 1317334378,
            'fee'           => '-0.0005',
            'account'       => '',
            'confirmations' => 1138,
            'amount'        => '-2.9995',
            'address'       => '12uSZxgLimvHzh8AYhCyHS3KyCsAkWwUN6',
            'category'      => 'send'
        },
        {
            'amount' => 4,
            'txid' =>
              '5377f0a25a414971409755dfa66fc9b29e76f2992f5e651cf89645d806dd79af',
            'time'          => 1317699190,
            'account'       => '263',
            'confirmations' => 582,
            'category'      => 'receive',
            'address'       => '1NZYzyrc5b8HZ91j2LmzUo3fCwvzp9qeJy'
        },
        {
            'amount' => '1.26',
            'txid' =>
              '8e45fab021d7b14f5e04c15c5531453220bb9408fa0856ecef84700784076948',
            'time'          => 1317743978,
            'account'       => '264',
            'confirmations' => 523,
            'category'      => 'receive',
            'address'       => '1Dd2JRSMf5mz6VafUqZHXJ6B7J99CzttPE'
        },
        {
            'amount' => '3.63',
            'txid' =>
              'db1b561e62f53823658178a5b684cb982d4a15e8dc42fb77f22c0310f91de6f4',
            'time'          => 1317788785,
            'account'       => '266',
            'confirmations' => 460,
            'category'      => 'receive',
            'address'       => '1EDuiDj1UhVSpZk1Bgz8Wfxizv5xw2ztBS'
        },
        {
            'amount' => '1.32',
            'txid' =>
              '788baf497518a11ec043862be9b9daeddf8588e135c87d9eccd1a0bdb1090bba',
            'time'          => 1317822039,
            'account'       => '270',
            'confirmations' => 409,
            'category'      => 'receive',
            'address'       => '16sp6jBmTm7g5NVW6wHAd7zGKhUWmp2hJB'
        },
        {
            'txid' =>
              'f41411a2808f7bea3733478604856c0fa119cab7d0b8468e4d571f12b25975e8',
            'time'          => 1317822684,
            'fee'           => '-0.0005',
            'account'       => '',
            'confirmations' => 407,
            'amount'        => '-10.2095',
            'address'       => '1BJQvf5YR2cn4bYbuNHXHD4De2cZEDLXc5',
            'category'      => 'send'
        }
    ]
    );
    
    print Dumper $array_ref;
    
    foreach my $hash_ref ( @$array_ref ) {
        foreach my $key ( keys %$hash_ref ) {
            print "$key => $hash_ref->{$key}\n";
        }
    }

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    If you wanted to loop over the array indexes, you'd change it to this:
    Code:
    my $array_size = scalar @$array_ref;
    
    foreach my $i ( 0..$array_size ) {
        foreach my $key ( keys %{ $array_ref->[$i] } ) {
            print "$key => $array_ref->[$i]{$key}\n";
        }
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •