in reply to Writing indexed arrays in to an file and then retrieved

Using Storable:

use Storable; store(\@array, 'file.ext'); my $aref = retrieve('file.ext');

JSON is also a good choice, and it's cross language/platform. ie. I can get the data from the file in Python after writing it in Perl:

use warnings; use strict; use JSON; my @array = qw(1 2 3); open my $wfh, '>', 'file.ext' or die "can't open file for writing: $!"; print $wfh encode_json(\@array); close $wfh; open my $fh, '<', 'file.ext' or die "can't open file for reading: $!"; my $aref = decode_json(<$fh>); close $fh;

Replies are listed 'Best First'.
Re^2: Writing indexed arrays in to an file and then retrieved
by perlmuser (Novice) on Oct 29, 2015 at 06:37 UTC

    Thanks Stevieb for the response .. and everyone else .. But somehow it does not work .. or may be i am not doing it right If i print the reg_map variable using the following code

    for $i ( 0 .. $page_counter-1 ) { print " ###### HI $i #### \n"; eval ( "\$Total_Size = \$\#reg_map${i}"); print " Total Size = $Total_Size\n"; for $j1 ( 0 .. $Total_Size ) { eval ("\$Num_elements_in_row = \@\{\$reg_map${i}\[\$j1\]\}"); print " Num_elements_in_row $j1 is = $Num_elements_in_row\n"; for $j3 ( 0 .. $Num_elements_in_row ) { eval("print \"Bit 1 :\$i:\$j1:\$j3 is \$reg_map${i}\[\$j1\]\[\$j +3\]\n\""); } } }

    I get the following display

    Total Size = 80

    Num_elements_in_row 0 is = 3

    Bit 1 :0:0:0 is NONE

    Bit 1 :0:0:1 is NONE

    Bit 1 :0:0:2 is NONE

    Bit 1 :0:0:3 is

    Num_elements_in_row 1 is = 4

    Bit 1 :0:1:0 is TEMP

    Bit 1 :0:1:1 is 0

    Bit 1 :0:1:2 is .

    Bit 1 :0:1:3 is 1

    Bit 1 :0:1:4 is

    I used Store as follows

     store(\@reg_map, 'file.ext');my $aref = retrieve('file.ext');

    When i print aref using the code below

    for $i ( 0 .. $page_counter-1 ) { print " ###### HI $i #### \n"; eval ( "\$Total_Size = \$\#aref${i}"); print " Total Size = $Total_Size\n"; for $j1 ( 0 .. $Total_Size ) { eval ("\$Num_elements_in_row = \@\{\$aref${i}\[\$j1\]\}"); print " Num_elements_in_row $j1 is = $Num_elements_in_row\n"; for $j3 ( 0 .. $Num_elements_in_row ) { eval("print \"Bit 2 :\$i:\$j1:\$j3 is \$aref${i}\[\$j1\]\[\$j3\] +\n\""); } } }

    I get the following

    ###### HI 0 ####

    Total Size = -1

    ###### HI 1 ####

    Total Size = -1

    I Also tried using JSON as follows

    open my $wfh, '>', 'file1.ext' or die "can't open file for writing: $!"; print $wfh encode_json(\@reg_map); close $wfh; open my $fh, '<', 'file.ext' or die "can't open file for reading: $!"; my @aref = decode_json(<$fh>);

    But i get an error at the last line :

    malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "pst0\x{4}\x{8}\x{8}1...") at perl_script.pl line 100, <$fh> line 1.

    Any thoughts on this ..

       eval "..."

      now is a good time to stop writing code like that :)