Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Reading anonymous array from file

by sherab (Scribe)
on Mar 10, 2014 at 17:57 UTC ( [id://1077727]=perlquestion: print w/replies, xml ) Need Help??

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

Hi folks, In all the years I have worked with perl I have managed to figure out how to slurp into a variable and an array but I need to create a script that reads an external file that contains an anonymous array and then work with it. Here's an example....

The file "data.txt" would contain this..
$VAR1 = [ 'Snow', 'White', '1313 Mockingbird Lane']
How would I read this in to an array to get this...
@array=("Snow","White","1313 Mockingbird Lane")
My apologies because I know this is probably so simple...

Replies are listed 'Best First'.
Re: Reading anonymous array from file
by McA (Priest) on Mar 10, 2014 at 18:02 UTC

    Hi,

    have a look at perldoc do.

    my $ref = do 'data.txt'; my @array = @$ref;

    McA

      Thank you so much. Man, I know that this was so simple and I really appreciate the perldoc reference. :-)
Re: Reading anonymous array from file
by davido (Cardinal) on Mar 10, 2014 at 18:08 UTC

    The JSON spec allows anonymous arrays at the top level.:

    s$ perl -MData::Dumper -MJSON::Tiny=j -E 'say Dumper j(q{["snow","whit +e","1212whatever"]})' $VAR1 = [ 'snow', 'white', '1212whatever' ];

    But your data is saved as the output from Data::Dumper, and the "$VAR1 =" portion isn't JSON. If you can't change your storage format, you will probably need to revive that with eval.

    Check out the documentation for Data::Dumper.


    Dave

Re: Reading anonymous array from file
by kcott (Archbishop) on Mar 11, 2014 at 00:45 UTC

    G'day sherab,

    I see ++McA has provided a solution for reconstructing the data stored by (I presume) Data::Dumper.

    If the creation of 'data.txt' is also under your control, you might condsider the built-in module Storable. Here's an example usage:

    #!/usr/bin/env perl -l use strict; use warnings; use Storable; my $file = './data.txt'; my @original_array = ('Snow', 'White', '1313 Mockingbird Lane'); store \@original_array => $file; my @retrieved_array = @{retrieve($file)}; print for @retrieved_array;

    Output:

    Snow White 1313 Mockingbird Lane

    -- Ken

Re: Reading anonymous array from file
by hippo (Bishop) on Mar 10, 2014 at 18:05 UTC

    The "simple" answer is eval, but there are a lot of caveats with that approach so be sure that you are aware of them. It would probably be better if you could amend the script which creates the data file in the first place. Good luck, anyway.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1077727]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found