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

Re: Replacing expression on certain line numbers

by karavelov (Monk)
on Jul 14, 2008 at 08:29 UTC ( #697414=note: print w/replies, xml ) Need Help??

in reply to Replacing expression on certain line numbers

Another option is to use list for the line numbers. The algorithm scans only once through data file

# first read and numericaly sort the lines my @lines = sort {$a <=> $b} <>; # then loop over data on STDIN # and write it to STDOUT my $line = shift @lines; while (<>){ if ($.==$line){ s/foo/bar/g; $line = shift @lines; } print; }
then you could use it like filter on UNIX-like OS-es:
cat data | ./script index-file > result

Best regards

P.S. Code is not tested

Replies are listed 'Best First'.
Re^2: Replacing expression on certain line numbers
by shmem (Chancellor) on Aug 03, 2008 at 22:00 UTC
    cat data | ./script index-file > result

    To avoid the "useless use of cat award"

    ./script index-file < data > result



    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re^2: Replacing expression on certain line numbers
by FunkyMonk (Chancellor) on Jul 14, 2008 at 15:32 UTC

    I didn't realise how deep the magic of <> was.

    perlop says:

    The <> symbol will return undef for end-of-file only once. If you call it again after this, it will assume you are processing another @ARGV list, and if you haven't set @ARGV, will read input from STDIN.

    Unless I state otherwise, all my code runs with strict and warnings

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://697414]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-01-19 15:08 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (55 votes). Check out past polls.