The information given to you on this is sufficient to explain that the
repetition operator (x) takes a string on the left and a number on the
right (specifically a positive integer).  As you can tell, perl does not
make you say that a $foo is an integer or $foo is a string, but rather just
allows you to say $foo is a value.  It determines the type of $foo on the
fly.

$string x $number gives the desired result because the first character of
the string $number is a number.

So data types in perl are based on sequential characters.  If the $number
starts with a number, it is considered a numerical string and when used in
a numerical context (like on the right side of the repetition operator) it
looks at the characters in order and at the first non-number, it stops
processing the rest of the string and truncates the value for that
operation.

If $string started with a number, $number x $string would not be repeated 0
times, but because it does start with a non-number, it is truncated for the
operation (the value of $string is unchanged, but perl thinks of it
differently for that operation) .  Because "" is treated the same way
undef() is treated, it is occasionally treated as 0, so really what $number
x $string is saying is $number x "" or $number x 0.

That should hopefully clarify why perl functions this particular way.


## Z. Bornheimer


On Tue, Jan 1, 2013 at 6:02 PM, Goke Aruna <mykl...@gmail.com> wrote:

> Hello Singh,
> Chomp the $string and test again.
> Really sorry I cannot bottom post with BBerry phone.
>
> Regards
>
> On 12/29/12, Om Prakash Singh <torque.in...@gmail.com> wrote:
> > Hi All,
> >
> > I am new to perl and while going through the chapters of Learning perl,
> > i just came through one excessive, while i was able to complete it, i
> > just came across something that didn't work and i couldn't find any
> > explanation for it. Would be great if i get some help in understanding
> > this behavior.
> >
> > below is the code
> >
> > print "the string: ";
> > $string = <STDIN>;
> > print "the no.: ";
> > chomp($number = <STDIN>);
> > $result = $string x $number;
> > print "the result is:\n$result";
> >
> >
> > in the above code when i use
> > $result = $string x $number;
> > i get the right result, i.e. strings printed on two lines.
> >
> > but, when i use
> > $result = $number x $string;
> > I don't get any output at all, just wondering why this happens, any help
> > would be highly appreciable.
> >
> > Regards,
> > Omps
> >
>
> --
> Sent from my mobile device
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to