Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Adding Strings to an Array Permanently

by OxYgEn (Acolyte)
on Mar 02, 2004 at 00:17 UTC ( #333115=perlquestion: print w/ replies, xml ) Need Help??
OxYgEn has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to add a string to an array permanently, so that when I run the script again the array will contain a string that was added the previous time?
If there are any substitutes to this which require text files or something, that's fine, too.

Ok, well I can't get the readmore tags to work, so if an editor wants to get this, please do, but for now I'm going to enclose it in lined spaces; treat it as a readmore:

---------------------------------------------------
Basically all I'm trying to do here is get it to print all the strings in an array, and if i give it certain args, add or remove a string from that array. Any possible way this could be done is fine; it doesn't have to read specifically from the array or anything(it could read from a text file or something like that as well). Thanks for reading the readmore.

-------------------------------------------------------


Thanks,
OxYgEn

Comment on Adding Strings to an Array Permanently
Re: Adding Strings to an Array Permanently
by Roger (Parson) on Mar 02, 2004 at 00:28 UTC
    If it's data persistance you are looking for, have a look at the Storable, Data::Serializer, etc... modules. You do a freeze of your structure and then save the output string to an external file. When the script is run the next time, first read the file, then do a thaw to get back your structure. I like the Data::Serializer because it is flexible and has support for encryption as well.

      Uh, this is a bit embarrasing, but...
      I'm very new to perl. I don't think you can be much newer than me. Could you give me some little snippet of code with an example of what you're saying, Roger?

      Thanks again,
      OxYgEn
        #!/usr/bin/perl -w use strict; use IO::File; use Data::Serializer; use Data::Dumper; my @list; my $s = Data::Serializer->new( serializer => 'Storable', portable => 0, compress => 0, ); if (-s "./list.bin") { print "Load ice and thaw\n"; my $f = new IO::File "./list.bin", "r" or die; my $ice = do { local $/; <$f> }; @list = @{$s->thaw($ice)}; print Dumper(\@list); } else { print "Build list and freeze\n"; @list = qw/ 0 1 2 3 /; my $ice = $s->freeze(\@list); my $f = new IO::File "./list.bin", "w" or die; print $f $ice; }

Re: Adding Strings to an Array Permanently
by BrowserUk (Pope) on Mar 02, 2004 at 00:42 UTC

    Take a look at Tie::File. It comes as standard with some perl distributions.

    #! perl -lsw use strict; use Tie::File; tie my @array, 'Tie::File', 'array.file'; push @array, @ARGV; print for @array; __END__ P:\test>333115 the quick brown fox the quick brown fox P:\test>333115 the sixth sick shiek's sixth sheep was sick the quick brown fox the sixth sick shiek's sixth sheep was sick

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Adding Strings to an Array Permanently
by tzz (Monk) on Mar 02, 2004 at 16:25 UTC
    Have you considered Inline::Files?

    It can be very handy. Just store your array in an inline file and rewrite the file as needed.

    Ted

Re: Adding Strings to an Array Permanently
by prostoalex (Scribe) on Mar 03, 2004 at 00:41 UTC

    Quick and dirty, but not necessarily correct and elegant solution would be:

    1. to store some special string like "THE_END" as the last one in the array
    2. if the array changes, to open up the Perl script itself, read it line by line, and when you come across "THE_END" (your array end), substitute that with $new_string.", "THE_END" (or basically, concatenate "THE_END" to the new string and then append the whole string to the array
    3. save the file

    I know, I know, inefficient and quite dirty, but if you need it real fast (besides - of you need it really fast with no downbloadable modules available, like your computer is not online at the moment), that should work.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://333115]
Approved by Roger
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-08-30 22:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls