CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
print returned strings from subroutines


New User

Dec 11, 2012, 7:08 AM

Post #1 of 2 (1593 views)
print returned strings from subroutines Can't Post


I have this code:

if ($key eq 'uptime') {
my $fuptime = getFriendlyUptime($val);
print $fuptime; #print fuptime string
push(@vals, [8, $fuptime]);

sub getFriendlyUptime {
int($_[0]/(24*60*60)) . "d:" . ($_[0]/(60*60))%24 . "h:" . ($_[0]/60)%60 . "m";

But when i try to print @vals, $fuptime value is ARRAY(0x134...) and don't print correct string but only ARRAY(0X134...).

How can I solve this situation?

Thanks for all your help.

Veteran / Moderator

Dec 11, 2012, 8:15 AM

Post #2 of 2 (1589 views)
Re: [x13] print returned strings from subroutines [In reply to] Can't Post

Your subroutine is not really doing anything, as you compute a string but don't do anything with it.

Although it should work, it would be more explicit to do something like this:

sub getFriendlyUptime {  
my $duration = int($_[0]/(24*60*60)) . "d:" . ($_[0]/(60*60))%24 . "h:" . ($_[0]/60)%60 . "m";
return $duration;

Then, print $fuptime to see what you get, if the conversion if OK.

Then, I am not sure what you are trying to do with:

push(@vals, [8, $fuptime])

Why do you want to push an anonymous array with a constant value (8) into the @vals array? This is correct syntax, but probably does not do what you want.

If you want to set the array element with subscript 8, then you should do:

$vals[8] = $fuptime;

if you just want to store your value in the array, then:

push @vals, $fuptime;

But if you use:

push(@vals, [8, $fuptime])

then, to retrieve $fuptime, you need something like $vals[0][1] (while $vals[0][0] will contain '8'). I doubt that's what you want.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives