terjek has asked for the wisdom of the Perl Monks concerning the following question:
Hey fellow monks
Take a look of the following three perl samples, and ask yourself what you think the output will be before you try them.
perl -wle 'my @a=1..9; printf"$a[%d]\n", 4' perl -wle 'my @a=1..9; printf"$a[%2d]\n", 4' perl -wle 'my @a=1..9; printf"\$a[%d]\n", 4'
I know that putting the $a[] in the sprintf in this way is not a common use of sprintf and i could easily do this and it would work :
perl -wle 'my @a=1..9; printf"%d\n", $a[4]'
I'm just curiuos to why perl behaves this way. When i use \$a, then %d is interpreted as a printf conversion as it should, but when i'm using just $a, then it interpret %d as an hash.
Terje
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Weird ? behavior of printf / sprintf
by ysth (Canon) on Jul 12, 2007 at 08:45 UTC | |
|
Re: Weird ? behavior of printf / sprintf
by Anno (Deacon) on Jul 12, 2007 at 09:06 UTC | |
|
Re: Weird ? behavior of printf / sprintf
by citromatik (Curate) on Jul 12, 2007 at 08:42 UTC | |
by blazar (Canon) on Jul 13, 2007 at 09:51 UTC | |
|
Re: Weird ? behavior of printf / sprintf
by ikegami (Patriarch) on Jul 12, 2007 at 13:48 UTC | |
|
Re: Weird ? behavior of printf / sprintf
by atemon (Chaplain) on Jul 12, 2007 at 10:01 UTC | |
|
Re: Weird ? behavior of printf / sprintf
by dsheroh (Monsignor) on Jul 12, 2007 at 15:36 UTC |