Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Modify a txt file

by Marshall (Abbot)
on Oct 20, 2011 at 03:18 UTC ( #932546=note: print w/replies, xml ) Need Help??


in reply to Modify a txt file

Given that the input file has a very large number of lines, I would try to just process the file line by line (no slurping the file into a single $file_content or @lines).

Here is one way:

#/usr/bin/perl -w use strict; my $cur_num; while (<DATA>) { $cur_num = $1 if (/^(\d+)/); # new $cur num if line starts # with digits s/(\S+)$/$cur_num/; # substitute the non-spaces at the end # of the line with the cur_num print; #a blank line is not modified } =Prints 0 0 0 0 13 13 13 28 28 28 42 42 42 42 55 55 55 55 55 =cut __DATA__ 0 ASDF ASEE ASEE 13 DERG DREG 28 QWER QWER 42 WERT WERT WERT 55 QWEASD QWEASD QWEASD QWEASD

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932546]
help
Chatterbox?
[holli]: i saw that thread and figured it`s OPP value too high. i didnt realise its PM code
[LanX]: its the style attribute, I can't show you pm code, can only link to old everything docs of you want
[holli]: i'd like to keep my sanity, tyvm
[holli]: at least what's left off it :)
[LanX]: the+everything+ bible

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-11-20 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (294 votes). Check out past polls.

    Notices?