Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Edit varaibles in file

by indie_campbell (Novice)
on Mar 31, 2004 at 14:08 UTC ( [id://341290]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, help on this is much needed. The following code works fine
local $/= "# input for "; while (<FILE1>) { s/1\.00/5/g if /Data\b\n/; print; }
input file looks like:
# input for Data 1.00 0.99 0.92 1.00 -0.22 0.61

But now can i adjust the code so that it only edits 1.00 on first line only. This file goes on with Data1 Data2 etc with different pairs of lines. Thanks

20040331 Edit by Corion: Added formatting, code tags

Replies are listed 'Best First'.
Re: Edit variables in file
by Roy Johnson (Monsignor) on Mar 31, 2004 at 15:43 UTC
    How about using a flip-flop operator?
    while (<DATA>) { /# input for/ .. s/1\.00/5/g; print; } __DATA__ # input for Data 1.00 0.99 0.92 1.00 -0.22 0.61
    or, to avoid warnings about "useless" use of flop,
    use strict; use warnings; while (<DATA>) { if (/# input for/ .. /\d/) { s/1\.00/5/g } print; } __DATA__ # input for Data 1.00 0.99 0.92 1.00 -0.22 0.61

    The PerlMonk tr/// Advocate
Re: Edit varaibles in file
by ccn (Vicar) on Mar 31, 2004 at 14:13 UTC
    local $/= "# input for "; my $done = 0; while (<FILE1>) { s/1\.00/5/g if /Data\b\n/ and !$done++; print; }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Edit varaibles in file
by dragonchild (Archbishop) on Mar 31, 2004 at 14:30 UTC
    In general, if you have special handling for the first line, you should handle the first line separately. This also makes it easier for people reading your code in six months. By breaking it up into two sections, you make it obvious that the first line is special (for whatever reason).

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Edit varaibles in file
by DamnDirtyApe (Curate) on Mar 31, 2004 at 19:11 UTC

    Personally, I think I'd take this approach:

    use Tie::File; tie my @arr, 'Tie::File', 'myfile.dat'; s(1\.00)(5) and last for @arr; untie @arr;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche

Log In?
Username:
Password:

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

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

    No recent polls found