Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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"};




----- 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/
>
>
>


Report this thread to moderator Post Follow-up to this message
Old Post
itshardtogetone@hotmail.com
04-02-08 09:30 AM


RE: how to extract the last digit
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"};




Report this thread to moderator Post Follow-up to this message
Old Post
Sanket Vaidya
04-02-08 01:08 PM


Re: how to extract the last digit
From:           	<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


Report this thread to moderator Post Follow-up to this message
Old Post
Jenda Krynicky
04-02-08 01:08 PM


RE: how to extract the last digit
From: "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


Report this thread to moderator Post Follow-up to this message
Old Post
Jenda Krynicky
04-02-08 01:08 PM


Re: how to extract the last digit
itshardtogetone@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

Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
04-03-08 12:19 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): « 1 [2]
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:40 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.