Find the answer to your Linux question:
Results 1 to 3 of 3
Hello everyone I'm trying to print the uptime of a computer. First, I split the time at : and put it in an array: Code: my @time = split(/:/, $uptime); ...
  1. #1
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277

    the print command

    Hello everyone

    I'm trying to print the uptime of a computer. First, I split the time at : and put it in an array:

    Code:
    my @time = split(/:/, $uptime);
    Then I simply print it:
    Code:
    $t=print ("@time[0]hours and @time[1]min");  
    print $t;
    The output I get is:
    20hours and 25
    min


    I don't understand why min is not on the same line as the rest of the string. I haven't included \n anywhere. Why is it printing min in a new line?

    Can anyone please explain to me what I'm doing wrong?

    Thank you

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So first off, I have no idea what programming language you're using, so I'm a bit confused.

    But anyway, where did you get the value in $uptime? What I suspect is happening is that $uptime stores the value "HH:MM\n". Then, when you split on the ':', the newline ends up in the second part when you print.

    Your code looks somewhat Perl-like (though if it's Perl, you're using "print" wrong), and Perl has a function called "chomp" which specifically removes newlines from the end of strings. Otherwise, some more information would be helpful.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux User
    Join Date
    Jul 2007
    Location
    Greece
    Posts
    277
    Hi Cabhan,

    Sorry for not mentioning the programming language but you guessed it right.
    You are correct, I just chomped $time[1] and is working fine now.

    Thanks a lot for your reply.

Posting Permissions

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