RuneK has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have an issue with Storable and retrieving data back into an array from a stored array.
Code:
use Storable; use strict; my @array1 = ('a','b','c','d'); my @array2 = (); print $#array1."\n"; store(\@array1,"file") or die "Cannot store\n"; @array2 = retrieve("file") or die "Cannot retrieve\n"; print $#array2."\n";
Output is:
3
0

So when I retrieve data from the file (which is there and has a size) nothing comes into @array2. How come? Is it related to that @array2 and $#array2 doesn't refer to the same "thing"?

Question is: How to I retrieve my data back into the @array2 so that it is equal to @array1?

Thanks in advance, Rune

Replies are listed 'Best First'.
Re: Can't retrieve with storable
by Joost (Canon) on Sep 15, 2004 at 08:39 UTC
Re: Can't retrieve with storable
by zejames (Hermit) on Sep 15, 2004 at 08:40 UTC
    You store a array reference, so you must retrieve an array reference too :

    store(\@array1,"file") or die "Cannot store\n"; @array2 = @{retrieve("file")} or die die "Cannot retrieve\n"; print $#array2."\n";

    HTH

    --
    zejames
Re: Can't retrieve with storable
by borisz (Canon) on Sep 15, 2004 at 08:45 UTC
    retrieve returns a reference to your data _not_ the data.
    my $aref = retrieve("file") or die "Cannot retrieve\n"; @array2 = @$aref; print $#array2."\n";
    Boris