[comp.lang.perl] uptime

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (05/31/91)

I was wondering whether there's any nicer ways to do this:

$usavgs = `/usr/ucb/uptime`;

if ($usavgs =~ /.*,(.*)user.*average: *(.*),(.*),(.*)/) {
	$users = $1;
	$avg1 = $2; $avg2 = $3; $avg3 = $4;
}

print $users, "\t", $avg1, "\t",  $avg2, "\t", $avg3, "\n";


I want to get the number of users, and the load averages into different
variables so I can print them out nicely for another program to scanf.

I was originally thinking of doing some split()s and/or substr()s, but that
would seem to take longer. How does this method compare to others?

		-Kartik


--
internet% whoami

subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

tchrist@convex.COM (Tom Christiansen) (05/31/91)

From the keyboard of subbarao@phoenix.Princeton.EDU (Kartik Subbarao):
:I was wondering whether there's any nicer ways to do this:
:
:$usavgs = `/usr/ucb/uptime`;
:
:if ($usavgs =~ /.*,(.*)user.*average: *(.*),(.*),(.*)/) {
:	$users = $1;
:	$avg1 = $2; $avg2 = $3; $avg3 = $4;
:}
:
:print $users, "\t", $avg1, "\t",  $avg2, "\t", $avg3, "\n";
:
:
:I want to get the number of users, and the load averages into different
:variables so I can print them out nicely for another program to scanf.
:
:I was originally thinking of doing some split()s and/or substr()s, but that
:would seem to take longer. How does this method compare to others?

Go wish the regexp: remember, one of the things that
perl is *about* is pattern matching.  Don't be afraid 
of using it.  Avoid splits.

print join("\t", `uptime` =~ /.*,(.*)user.*average: *(.*),(.*),(.*)/), "\n";

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time."