in reply to anyway to combine these two steps?

sprintf("%02d", $num =~ /20(\d)/);

Note that $num =~ /20(\d)/ returns in list context the matches, and an empty list when there are none. So if you need to check whether a match occured, you can't do that here. However, you can provide to sprintf a default value, since it only uses the values it needs, and if there's an extra one it's ignored:

sprintf("%02d", ($num =~ /20(\d)/), 0);

A general example: print sprintf("%s\n", (/(bar)/), 'default') for qw(foo bar)

Replies are listed 'Best First'.
Re^2: anyway to combine these two steps?
by Anonymous Monk on Jan 19, 2010 at 07:07 UTC
    Another method, from the unnecessary ternary operator dept:

    $output = (/20(\d)/ ? sprintf("%02d", $1) : format_error("that isn't valid data!"));