- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- quick perl question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 01:28 PM
12-03-2003 01:28 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 01:29 PM
12-03-2003 01:29 PM
Re: quick perl question
I have posted this in the wrong section!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 02:12 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 02:19 PM
12-03-2003 02:19 PM
Re: quick perl question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 02:29 PM
12-03-2003 02:29 PM
Re: quick perl question
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 02:31 PM
12-03-2003 02:31 PM
Re: quick perl question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 02:37 PM
12-03-2003 02:37 PM
Re: quick perl question
Thanks
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 06:50 PM
12-03-2003 06:50 PM
Re: quick perl question
> 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 01:58 AM
12-04-2003 01:58 AM
Re: quick perl question
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 02:11 AM
12-04-2003 02:11 AM
Re: quick perl question
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 :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 05:58 AM
12-04-2003 05:58 AM
Re: quick perl question
Steve