in reply to In place edit and printf

You are not giving printf a value for the %s token. Either pass in $' as the next arg, or replace %s in the picture with $'. Also, this may have been a retype error, but you probably don't want the $ at the end of your regex since there is more text to follow the digits.

while (<>) { m/^(\w+)\s+(\d+)(.*)$/ or die; # You should # always check a regex before # using $1, etc. printf "%s %d %s", $1, (exists $disk{$1} ? 0 : $2+$partition), $3 +; }

Updated: Removed $1 from the picture per suggestion below.

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

Replies are listed 'Best First'.
Re^2: In place edit and printf
by ikegami (Patriarch) on Dec 09, 2005 at 16:47 UTC

    Your solution has two problems:

    1) Injection Vulnerability

    You don't escape the %s in $1 and $'. Pass them as arguments to printf instead, adding %s in the format string where necessary

    2) Performance Leak

    Using $' can cause other (unrelated) regexp in your program to slow down. Add (.*) to the end of the regexp and use $2 instead of $'.

      You would think with all the recent noise about the printf bug I would have remembered that. I was too focused on the initial question at hand.

      Ted Young

      ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re^2: In place edit and printf
by Anonymous Monk on Dec 12, 2005 at 09:06 UTC
    Thank you for the answer. I've taken note of the performance and vulnerability issues mentioned.