1833967 Members
2015 Online
110063 Solutions
New Discussion

Re: quick perl question

 
SOLVED
Go to solution
steven Burgess_2
Honored Contributor

quick perl question

Hi

Easy points here

I'm only on chapter2 but strugglng with this little question

I have a scalar variable $total which is 456.5899

I want to use the int operator to give me the integer portion of $total. ie, passing the variable to int then printing the new total.

I have tried a number of permitations but just can't get it right

Can anyone help ?

Cheers

Steve
take your time and think things through
10 REPLIES 10
steven Burgess_2
Honored Contributor

Re: quick perl question

whoops

I have posted this in the wrong section!
take your time and think things through
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: quick perl question

my $total = 456.5899;
print int($total),"\n";

or

printf ("%d\n",int($total));
If it ain't broke, I can fix that.
steven Burgess_2
Honored Contributor

Re: quick perl question

Hi A.Clay

The problem I'm having is that i'm doing

print "The sum is int($total),"\n";

Which doesn't actually display the integer but the whole total

see where i'm coming from ?

thanks

Steve
take your time and think things through
A. Clay Stephenson
Acclaimed Contributor

Re: quick perl question

Okay, perl seems a little wierd until you understand how it handles variables:

print "The sum is : ",int($total),"\n";

By the way, you should note that Perl always uses floating point math unless you specifically tell it not to via the
use integer;
staement.

You can put that inside a function and use integer; will only apply to the enclosing function.
If it ain't broke, I can fix that.
steven Burgess_2
Honored Contributor

Re: quick perl question

Hi

I got to this in the end, which was a suggestion

my $total=int($total);
chomp $total;

print "After $mons months, at $interest monthly you\n";
print "will have $total\n";


That though is what i considered to be the long way round, i'll give yours a go and respond

thanks again

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: quick perl question

Thats cool

Thanks

Steve
take your time and think things through
H.Merijn Brand (procura
Honored Contributor

Re: quick perl question

You almost got it :)

> my $total=int($total);

correct. parens are optional

> chomp $total;

humbug. the chomp operator cuts off the optional line ending (\n on unix) from a string. Now that you've just asigned an integer (numeric) to $total, you can rest asured that it has absolutely no line ending.
int is a numerec operator, chomp is a string operator

> print "After $mons months, at $interest monthly you\n";
> print "will have $total\n";

Good. When using double quotes, all *variables* used inside the string are interpolated.

Now for the curious amongst you: it *is* posible to use interpolated functions inside double quoted strings :)

print "will have @{[int$total]}\n";

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ralph Grothe
Honored Contributor

Re: quick perl question

Just an attempt to explain the reference magic showed to you by procura.
This is probably the way the Perlians do it but very scaring to Perl newbies.
Especially they should read the POD because it's all explained therein.
Here's an excerpt from "perldoc perlref"


Here's a trick for interpolating a subroutine call into a string:

print "My sub returned @{[mysub(1,2,3)]} that time.\n";

The way it works is that when the "@{...}" is seen in the double-
quoted string, it's evaluated as a block. The block creates a
reference to an anonymous array containing the results of the call to
"mysub(1,2,3)". So the whole block returns a reference to an array,
which is then dereferenced by "@{...}" and stuck into the double-
quoted string. This chicanery is also useful for arbitrary
expressions:

print "That yields @{[$n + 5]} widgets\n";


Just a word of caution.
Yes, procura is right you can ommit the parentheses of function if they were predeclared (either built-in, or by the "subs" pragma).
And this you will see in code of frequent Perl hackers.
But this can be very tricky, and you can easily fall victim to precedence.
E.g. if you don't distinguish between the "or" and "||" operator this may lead to strange results when you omitted parentheses.
Precedence itself can get very tricky especially when using convoluted compound statements.
See "perldoc perlop"
Moral, when in doubt, be boring and explicit.

Madness, thy name is system administration
Mark Grant
Honored Contributor

Re: quick perl question

Thanks for that Ralph!

I have to admit, when I see stuff like that my extremly safe and boring side comes to the fore as I think "If I see what I have just written, will I understand it next week".

I have a rather large script that I wrote years ago and I still haven't worked out how one of the lines I wrote works. I just leave it here becuase I know it does :)
Never preceed any demonstration with anything more predictive than "watch this"
steven Burgess_2
Honored Contributor

Re: quick perl question

Thanks for the responses everyone

Steve
take your time and think things through