Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Persistent data storage

by ssinha (Novice)
on Aug 16, 2012 at 12:45 UTC ( [id://987754]=perlquestion: print w/replies, xml ) Need Help??

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

I need to store user inputs using some data structure and then when the code reruns modify the user inputs taken in the previous run. Ive tried storing data in array and then storing the array in a file and then retrieved the array from the file but i am unable to modify the read contents according to my specifications. I am posting what i have done. Please help

arr1=("File1","User1"); @arr2=("File2","User2"); open fin,"+<","arr.txt" or die "error"; print fin "@arr1\n"; print fin "@arr2"; print "Enter filename:"; $fname=<>; close(fin); open fin,"+<","arr.txt" or die "error"; chomp($fname); while($line=<fin>) { print "\nReading"; print "\n$line"; if($line =~ m/$fname\s/) { print "\nEntered if"; chomp($line); print "\n$line"; @read=$line; push(@read,"User3"); $line="@read"; print "\n$line"; print fin "@read"; } } close(fin);

Replies are listed 'Best First'.
Re: Persistent data storage
by blue_cowdawg (Monsignor) on Aug 16, 2012 at 13:59 UTC
        Ive tried storing data in array and then storing the array in a file and then retrieved the array

    Take a look at Tie::File. That module allows for functionality such that you can treat the lines in a file the same as you would an array. No, it does not load the entire file into memory.

    #!/usr/bin/perl -w ##################################################### use strict; use Tie::File; tie my @ry,"Tie::File","myfile.txt"; my @data=( [ 'apple',12.2,'red' ], [ 'bannana', 33.3,'yellow'], [ 'pineapple', 17.2,'green'] ); foreach my $dat(@data){ push @ry,join(",",@{$dat}); } untie @ry; tie @ry,"Tie::File","myfile.txt"; my $t= $ry[1]; $ry[1]=$ry[0]; $ry[0]=$t; untie @ry;
    The code above gives you a file that has this for contents:
    bannana,33.3,yellow apple,12.2,red pineapple,17.2,green

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Thank you everyone for the help. I will try using Tie:File and see if that works.

        I am trying to use Tie:File but while running it is giving an error : Can't locate object method "TIEARRAY" via package "Tie:File" . I tried searching on the net but did not get any valid solutions. I have checked if Tie:File is installed and it is; so is Tie:Array. I do not know where i am going wrong. Kindly help.

Re: Persistent data storage
by Athanasius (Archbishop) on Aug 16, 2012 at 13:45 UTC

    Hello ssinha, and welcome to the Monastery!

    You are trying to modify a text file in-place, but are using the wrong method. Consider the following extract from the documentation for open:

    You can put a + in front of the > or < to indicate that you want both read and write access to the file; ... You can't usually use either read-write mode for updating textfiles, since they have variable-length records.

    Here are some strategies you can use:

    1. Open the data file for reading, and a different (temporary) file for writing. Process the first file line-by-line, writing each new line to the temp file. When done, rename the temp file with the name of the data file.
    2. Same as (1) but done implicitly via the -i command switch. See Command Switches.
    3. Open the file, and tie it to an array using the core module Tie::File.

    Alternatively, depending on your needs, you may be well-advised to pursue the database solution recommended by sundialsvc4 above.

    Hope that helps,

    Athanasius <°(((><contra mundum

Re: Persistent data storage
by flexvault (Monsignor) on Aug 16, 2012 at 13:59 UTC

    Welcome ssinha,

    Maybe you typed the code in, but as displayed it doesn't run. I fixed a few things, but the code needs a lot more fixing. Try adding 'use strict;' and 'use warnings;'. Fix up the code, and then post the actual 'working' version. Also, do a 'super search' on 'hash examples' since it may be easier to solve your problem.

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

Re: Persistent data storage
by sundialsvc4 (Abbot) on Aug 16, 2012 at 13:11 UTC

    I find SQLite database-files to be extremely useful in this sort of situation.   They are, as I said, files, and SQLite is a public domain(!) format that is used on everything everywhere.   You can store and retrieve your data using DBI, having encoded and decoded the data using something like JSON or Storable.   You also have the full power of SQL queries at your disposal, which is often very convenient for organizing the data, for keeping the results of multiple runs, for potentially using the data with applications other than Perl, and so forth.   (The one gotcha that you do need to know about SQLite is that you need to wrap database operations in a transaction, because otherwise, by design, it reads-and-verifies every disk write.   Physical disk-writes for transactions are usually deferred, “lazily,” until the transaction ends.)

    I suggest this as a “better-all-around in the long run” approach to your problem that has worked very well for me.

Re: Persistent data storage
by philiprbrenan (Monk) on Aug 29, 2012 at 21:54 UTC

    Have you considered using Storable?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found