Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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.



Replies are listed 'Best First'.
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 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,
        #!/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 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.


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://333115]
Approved by Roger
Front-paged by broquaint
[Corion]: A good weekstart to everybody. I hope you had a good Pentecost
marto was working yesterday
[Corion]: marto: Ow :(
[marto]: Corion, not to worry, it was ok :)
[Corion]: I "just" had a call from a friend whom I host the website for, and they were quite hectic yet uninformed about what they'd have to change for GDPR... This felt like work to me, but at least I got that call in a café, not in the office

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2018-05-22 07:50 GMT
Find Nodes?
    Voting Booth?