On Jun 18, 11:27 am, [EMAIL PROTECTED] (Graeme McLaren)
wrote:
> Hi all, I'm in need of a loop, can't seem to find what I'm looking for 
> online.  I want to loop over this array ref inserting each hash value into 
> the DB at each iteration.I've got the following data structure which is 
> assigned to $aref:
>
> $VAR1 = [          {            'rate' => '1.98',            'name' => 
> 'Dollars -> Sterling'          },          {            'rate' => '1.5',      
>       'name' => 'Sterling -> Euros'          },          {            'rate' 
> => '0.75',            'name' => 'Dollars -> Euros'          },          {     
>        'rate' => '2.05',            'name' => 'Sterling -> Dollars'          
> },          {            'rate' => '0.72',            'name' => 'Euro -> 
> Sterling'          },          {            'rate' => '1.52',            
> 'name' => 'Euro -> Dollars'          },          {            'rate' => 
> '1.98',            'name' => 'Amex Exchange Rate'          }        ];
>
> Can anyone help out with this?  
>
> Much appreciated.
>
> G :)


Since you are dealing with an array of hashes you can use foreach to
loop over your data structure.


use strict;
use warnings;


my $VAR1 = [
{
    'rate' => '1.98',
    'name' => 'Dollars -> Sterling'
},          {            'rate' => '1.5',
'name' => 'Sterling -> Euros'          },          {            'rate'
=> '0.75',            'name' => 'Dollars ->
Euros'          },          {            'rate' => '2.05',
'name' => 'Sterling -> Dollars'          },
{            'rate' => '0.72',            'name' => 'Euro ->
Sterling'          },          {            'rate' =>
'1.52',            'name' => 'Euro -> Dollars'          },
{            'rate' => '1.98',            'name' => 'Amex Exchange
Rate'          }        ];




foreach my $entry (@$VAR1){
    print "Entry\n";
    print "  Name:  $entry->{name}\n";
    print "  Rate:  $entry->{rate}\n";
    my $statement = "INSERT \'$entry->{name}\', \'$entry->{rate}\'
INTO your_table";
    print "$statement\n";
}


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to