Closed Thread
Results 1 to 5 of 5

Thread: sumthing not working here

  1. #1
    Waffles!! GregT is on a distinguished road GregT's Avatar
    Join Date
    Feb 2002
    Location
    Michigan
    Posts
    2,496

    sumthing not working here

    Code:
    sub uptime {
    
    unless($^O =~ m/^Win32$/i) {
    
    open(UPTIME, "</proc/uptime") || die "Couldn't open /proc/uptime for reading!\n";
    $uptime = <UPTIME>;
    close(UPTIME);
    
    $uptimemin = $uptime/60%60;
    $uptimehour = $uptime/3600%24;
    $uptimeday = $uptime/86400;
    @uptimeday = split(/./, $uptimeday);
    $uptimeday = $uptimeday[0]; #give it a nicer name !
    
    if($uptimeday gt "0") {
    
    print "Server up for $uptimeday day\(s\), $uptimehour hour\(s\), and $uptimemin minute\(s\)\n";
    
    } else {
    
    print "Server up for $uptimehour hour\(s\) and $uptimemin minute\(s\)\n";
    is leaving $uptimeday[0] blank ! whats wrong ?
    Insert lame joke here.

  2. #2
    NLC Dusty is an unknown quantity at this point Dusty's Avatar
    Join Date
    Oct 2000
    Posts
    2,953
    @uptimeday=split(/./,$uptimeday);

    Do you mean /\./ (a period) instead of /./ (any character except a newline)?


    if($uptimeday gt "0") {

    If $uptimeday is a number, then you should use "if($uptimeday>0){".


    print "... day\(s\) ...

    You don't need to escape that, "day(s)" is fine. Same for hour and minute.


    You're also missing three curly brackets at the end, but I'll assume you just didn't copy them and they are actually there.


    Also, while there isn't anything wrong with:

    @uptimeday = split(/\./, $uptimeday);
    $uptimeday = $uptimeday[0]; #give it a nicer name !

    It is a bit much for such a simple task. It would be easier to use:

    $uptimeday=~s/\..*//;

    Or just cast it as an integer:

    $uptimeday=int($uptimeday);

    Unless you're going to use the decimal you truncate off later in the program, I wouldn't waste the memory storing it.
    I think I may have been here before... a long, long time ago... 599699 Unread Posts, you say?

    Ah, I found my old avatar. The hypnotic evil duck.
    It was 2002. Duck avatars were the style at the time.
    Long and fascinating story there. I'd tell you if I remembered it.

  3. #3
    Waffles!! GregT is on a distinguished road GregT's Avatar
    Join Date
    Feb 2002
    Location
    Michigan
    Posts
    2,496
    regexp wont work as the . is a decimal. thats why i used split. $uptimeday unmodified is 1.598300230430 for example.
    Insert lame joke here.

  4. #4
    Waffles!! GregT is on a distinguished road GregT's Avatar
    Join Date
    Feb 2002
    Location
    Michigan
    Posts
    2,496
    adding the \ before . did the trick, thanks
    Insert lame joke here.

  5. #5
    NLC Dusty is an unknown quantity at this point Dusty's Avatar
    Join Date
    Oct 2000
    Posts
    2,953
    regexp wont work as the . is a decimal.
    Sure it will. Just like the one in the split, all you have to do is escape the dot.
    I think I may have been here before... a long, long time ago... 599699 Unread Posts, you say?

    Ah, I found my old avatar. The hypnotic evil duck.
    It was 2002. Duck avatars were the style at the time.
    Long and fascinating story there. I'd tell you if I remembered it.

Closed Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts