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

Hello,

I am working on a project in which i have to work on txt file and want to edit few things like tables in it and save as new file.

I am saving whole data in a variable (scalar)

from it i am extracting table with regex and because of which my original data is getting modified.

To extract table i am using

$wholefile=~s/(pattern for table)/sub($&)/segx;

I want to save table in different scalar without modifying $wholefile, can anybody please tell me how to do it?

  • Comment on Extracting data from file and modifying without making change in file.
  • Download Code

Replies are listed 'Best First'.
Re: Extracting data from file and modifying without making change in file.
by Utilitarian (Vicar) on Aug 26, 2010 at 07:51 UTC
    Then don't apply a substitution, capture the (pattern for table) and evaluate. On a side note never use $`, $& and $' as they incur a large performance penalty on all regexes within your program.
    my $last_match=0; while ($wholefile =~ /(pattern for table)/){ $newfile .= substr($wholefile, $last_match, ($last_match - $-[0])); $newfile.=eval{sub($1)}; # I think this is what you want to do #of course it will always return 0, #but that's what you have in the regex above. # perhaps you meant $newfile.= "sub($1)"; $last = $+[0]; }
    The @-, $1, and @+ variables are a better way to manipulate your matches.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Extracting data from file and modifying without making change in file.
by locked_user sundialsvc4 (Abbot) on Aug 26, 2010 at 13:37 UTC

    Strange as it may seem, updating a file “in place” is very often really not what you want to do, for two reasons:

    1. Disk space is dirt-cheap and getting cheaper.
    2. You get two generations of the data:   a clear “before” and “after” version, which you can easily diff.

    If you make frequent modifications of the same kind to the data in a file, consider using a template-driven approach to the problem.   Packages like Template::Toolkit are most commonly used to build web pages, but they can do other things too.

Re: Extracting data from file and modifying without making change in file.
by Anonymous Monk on Aug 26, 2010 at 07:32 UTC
    what is a table?
Re: Extracting data from file and modifying without making change in file.
by suhailck (Friar) on Aug 26, 2010 at 07:45 UTC
    Is this what you are looking for??
    perl -le 'my @array;my $value="a0b1c2"; sub f1 { $_[0]+1 }; $value=~/(\d)(??{push @arr,f1($1)})/sg; print join "\n",@arr' 1 2 3
Re: Extracting data from file and modifying without making change in file.
by Marshall (Canon) on Aug 27, 2010 at 10:23 UTC
    If you don't want to modify $wholefile with a substitution, $wholefile_copy = $wholefile; and run substitution on the copy. Not trying to be snarky, but unless the file is humongous, the memory usage won't matter.
Re: Extracting data from file and modifying without making change in file.
by tej (Scribe) on Aug 26, 2010 at 08:16 UTC

    Hey,

    Thanks, Utilitarian (Deacon)