Code Comments
Programming Forum and web based access to our favorite programming groups.Thanks everyone for the help!
I get an error on 'chop' when I use the following. Why is this so?
Thanks
use strict;
use warnings;
my @eightdecks = 1..20;
my $last = chop ($eightdecks[0] + $eightdecks[2]);
if ($last =~ /[0-5]/){
print "yes match.\n";
}else{print "not match\n"};
----- Original Message -----
From: "John W. Krahn" <krahnj@telus.net>
To: "Perl Beginners" <beginners@perl.org>
Sent: Wednesday, April 02, 2008 12:06 AM
Subject: Re: how to extract the last digit
> itshardtogetone@hotmail.com wrote:
>
> Hello,
>
>
> my $last_digit = chop $number;
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
Post Follow-up to this message
Hi
You get the error on chop because anything given with chop function is
considered as string (In other words chop acts on string). So perl considers
the '+' operator you provide with chop as string rather than addition
operator.This is how your task can be accomplished.
use warnings;
use strict;
my @eightdecks = 1..20;
my $temp =$eightdecks[0] + $eightdecks[2];
my $last = chop($temp);
print "Temp = $last";
Hope this helps.
-----Original Message-----
From: itshardtogetone@hotmail.com [mailto:itshardtogetone@hotmail.com]
Sent: Wednesday, April 02, 2008 1:42 PM
To: beginners@perl.org
Subject: Re: how to extract the last digit
Thanks everyone for the help!
I get an error on 'chop' when I use the following. Why is this so?
Thanks
use strict;
use warnings;
my @eightdecks = 1..20;
my $last = chop ($eightdecks[0] + $eightdecks[2]);
if ($last =~ /[0-5]/){
print "yes match.\n";
}else{print "not match\n"};
Post Follow-up to this messageFrom: <itshardtogetone@hotmail.com> > Thanks everyone for the help! > I get an error on 'chop' when I use the following. Why is this so? > Thanks > > use strict; > use warnings; > my @eightdecks = 1..20; > my $last = chop ($eightdecks[0] + $eightdecks[2]); Because chop() attempts to modify its parameter. So it can only be called with a variable, not a more complex expression. Jenda ===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery
Post Follow-up to this messageFrom: "sanket vaidya" <sanket.vaidya@patni.com>
> You get the error on chop because anything given with chop function is
> considered as string (In other words chop acts on string). So perl conside
rs
> the '+' operator you provide with chop as string rather than addition
> operator.This is how your task can be accomplished.
>
> use warnings;
> use strict;
>
> my @eightdecks = 1..20;
> my $temp =$eightdecks[0] + $eightdecks[2];
> my $last = chop($temp);
> print "Temp = $last";
>
> Hope this helps.
Nope. + is addition in Perl. Always. No matter the context. You get
the same result with
my $tmp = 1 + 2;
print "result is " . $tmp . "\n";
as with
print "result is " . (1 + 2) . "\n";
Perl adds the two numbers with no problem and only then if needed
converts the result from a number to the string.
The reason of the error message
Can't modify addition (+) in chop
is simple. chop() tries to modify its parameter so the parametr must
be something that can be modified. It must be a "lvalue": $variable,
$array[$element], $hash{'element'},
$some{more}[$complex][$structure], ...
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
Post Follow-up to this messageitshardtogetone@hotmail.com wrote:
>
> Thanks everyone for the help!
> I get an error on 'chop' when I use the following. Why is this so?
> Thanks
>
> use strict;
> use warnings;
> my @eightdecks = 1..20;
> my $last = chop ($eightdecks[0] + $eightdecks[2]);
>
> if ($last =~ /[0-5]/){
> print "yes match.\n";
> }else{print "not match\n"};
You can only chop a variable, because the last character is removed from
the target string.
my $last = do {
chop(my $sum = $eightdecks[0] + $eightdecks[2]);
};
will do the trick. It assigns the sum to $sum and then chops $sum and
returns the last character. Or, more simply, you can just write
my $last = substr $eightdecks[0] + $eightdecks[2], -1;
HTH,
Rob
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.