PDA

View Full Version : storing an array in a session?


bazz
05-29-2008, 01:46 PM
Likely, because I want to, you can't :)

I ahve the array
@prices = qw( 65.25 65.30 65.50 ); # example not actual

I want to do this


$session->param('prices', @prices);
But i get an error

param(): usage error. Invalid syntax at script.pl line 360

Is it best done by joining the individual values into a $var and then after I recall it from the session, to 'split' etc?

bazz

FishMonger
05-29-2008, 05:36 PM
Use an array reference.
$session->param('prices', \@prices);

Quoted from the module's documentation.
param($name, $value)
param(-name=>$name, -value=>$value)

Used in either of the above syntax assigns a new value to $name parameter, which can later be retrieved with previously introduced param() syntax. $value may be a scalar, arrayref or hashref.

bazz
05-29-2008, 09:24 PM
Thank you FishMonger.

What I am trying to do is; I am trying to put two values into a hash and to store the whole hash in the session.

Only the key is being stored and I can't seem to undertsand why the value isn't.
Both the key and the value print as expected.

Can anyone help please?

here's the code


my %dates_and_price_hash;


foreach my $timestamp (sort @daylist) # run the loop for each date of the visit
{
my $iso = convert_to_iso($timestamp); # converts to iso format to match with db values
my ( $year, $month, $date ) = split "-", $iso, 3;

$sth->execute($room_type_id, $business_id, $iso, $iso, $day_number, $day_number );


while ( @fields = $sth->fetchrow_array )
{
push (@prices, $fields[1]); # needed for another function
#push (@full_array, @fields);
print qq( fields[1] = $fields[1] <br />); # correct
$dates_and_price_hash{$iso}{$fields[1]};
}

}

This 'should' be storing a key value pair, comprising iso-date and price(for that date). buit only the $iso value is in the dump.

here's the appropriate part of the dump


'dates_and_price_hash' => {
'2008-05-16' => {},
'2008-05-15' => {},
'2008-05-18' => {},
'2008-05-17' => {}
},


I would also like the data to be stroed in date order but I suppose that can be managed when the hash is being iterated through later?

Any pointer to what I am missing would be very welcome.

bazz

FishMonger
05-29-2008, 09:56 PM
Assuming that $fields[1] actually holds the desired data, your hash assignment should be:

$dates_and_price_hash{$iso} = $fields[1];

bazz
05-29-2008, 10:09 PM
excellent FishMonger.

I now get this to be stored


'dates_and_price_hash' => {
'2008-05-16' => '60',
'2008-05-15' => '65',
'2008-05-18' => '60',
'2008-05-17' => '60'
},

I'll work now on retrieving it :)

Thanks again.
bazz