Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > String and Array Programming in Perl

Reply
Thread Tools

String and Array Programming in Perl

 
 
DeveloperGuy
Guest
Posts: n/a
 
      10-12-2004
I am very very new to Perl and am trying automate a process in my AIX
Unix box. I issed the command ps -aef and sent it to a file. How do
I get how many different users running programs, the total time for
each user in hours:minutes format, and who is running the longest
process and the program name? I am not familiar with using the loops.
I know that I can probably use the date command to specify the date.
This is where I am stuck thus far. Please help anyone...

#! /usr/bin/perl

use strict;
use warnings;

@users;
@tmpfile = OPEN(DataFileHandle, /home/smallp/data.txt);

$tmpfile[0];
$users[0];

for ($count= 0; $count <= $#users; $count++ {
If $tmpline[0] eq $users[i]
if TRUE then exit
push(@users, $users[0]);
}
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-12-2004
DeveloperGuy <(E-Mail Removed)> wrote:

> I am very very new to Perl



We will still expect that you use Perl rather than something
merely Perlish-looking.


> and am trying automate a process in my AIX
> Unix box. I issed the command ps -aef and sent it to a file.



You can do that from within Perl itself, no need for a file.

my @ps_lines = `ps -aef`; # backwards single quotes
or
my @ps_lines = qx/ps -aef/; # backwards single quotes in disguise
or
open PS, 'ps -aef|' or die "could not run ps $!";
while ( <PS> ) ...


> How do
> I get how many different users running programs, the total time for
> each user in hours:minutes format, and who is running the longest
> process and the program name?



By parsing the output of the ps command.

You might want to use Perl's unpack() or substr() functions
to help you with that.


> I am not familiar with using the loops.



Then become familiar with using the loops, they are documented in:

perldoc perlsyn


> I know that I can probably use the date command to specify the date.



You can do that from within Perl too, no need for an external date program.

perldoc -f localtime
perldoc -f gmtime


> This is where I am stuck thus far. Please help anyone...
>
> #! /usr/bin/perl
>
> use strict;



When you put that in your programs you are making a promise:

I promise to declare my variables before using their short names.

If you break your promise, then perl will refuse to run your program.


> use warnings;
>
> @users;



You have not declared that variable, so perl refuses to run your program.

my @users;


> @tmpfile = OPEN(DataFileHandle, /home/smallp/data.txt);



Perl does not have an OPEN() function, only an open() function.

Case matters.

Put 'quotes' around your strings.

open() returns a single thing, no need for an array to hold its return value.

It is a convention to use all UPPER CASE for filehandles.

You should always, yes *always*, check the return value from open()
to ensure that you actually got what you asked for:

open DATA_FILEHANDLE, '/home/smallp/data.txt' or
die "could not open '/home/smallp/data.txt' $!";


Your code never makes use of the filehandle. You will need to *read*
from it to get the data to process...


> $tmpfile[0];
> $users[0];



Those are do-nothing statements, they have no useful effect.

What were you hoping those 2 lines of code would do for you?


> for ($count= 0; $count <= $#users; $count++ {



A more Perlish way to get the same thing is:

foreach my $count ( 0 .. $#users ) {


> If $tmpline[0] eq $users[i]



Perl does not have an "If" keyword, only an "if" keyword.

Case (still) matters.

You need (parenthesis) around the condition in an if statement.


> if TRUE then exit



Perl does not even have a "then" keyword, nor a "TRUE" keyword.

This is not Perl code. What language is it?



--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-12-2004
On 12 Oct 2004, you wrote in comp.lang.perl.misc:

> I am very very new to Perl and am trying automate a process in my AIX
> Unix box. I issed the command ps -aef and sent it to a file. How do
> I get how many different users running programs, the total time for
> each user in hours:minutes format, and who is running the longest
> process and the program name? I am not familiar with using the loops.
> I know that I can probably use the date command to specify the date.
> This is where I am stuck thus far. Please help anyone...


This means you need to bite the bullet and actually pay for a book. For
recommendations, go to http://learn.perl.org/

Now:

> #! /usr/bin/perl
>
> use strict;
> use warnings;


Good

> @users;


Not good:

D:\Home> perl -c t.pl
Bareword found where operator expected at t.pl line 7, near "/home/smallp"
(Missing operator before allp?)
Global symbol "@users" requires explicit package name at t.pl line 6.
Global symbol "@tmpfile" requires explicit package name at t.pl line 7.
syntax error at t.pl line 7, near "/home/smallp"
Global symbol "@tmpfile" requires explicit package name at t.pl line 9.
Global symbol "@users" requires explicit package name at t.pl line 10.
Global symbol "$count" requires explicit package name at t.pl line 12.
Global symbol "$count" requires explicit package name at t.pl line 12.
Global symbol "@users" requires explicit package name at t.pl line 12.
Global symbol "$count" requires explicit package name at t.pl line 12.
syntax error at t.pl line 12, near "++;"
t.pl has too many errors.

Why did you not fix this stuff before posting?

> @tmpfile = OPEN(DataFileHandle, /home/smallp/data.txt);


perldoc -f open

I think it was Tad who put it most eloquently: You can't just make s**t up
and expect it to work!

> $tmpfile[0];
> $users[0];


Huh?

> for ($count= 0; $count <= $#users; $count++ {
> If $tmpline[0] eq $users[i]
> if TRUE then exit


Huh???

> push(@users, $users[0]);
> }


I guess I'll take the bait anyway.

I have:

D:\Home> ps -v
PS (cygwin) 1.11
Process Statistics
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
Compiled on May 25 2004

and I get:

D:\Home> ps -aef
UID PID PPID TTY STIME COMMAND
hbb1 582167 1 con 16:09:39 /usr/bin/BASH
hbb1 565087 1 con 16:09:52 /usr/bin/PS

#! /usr/bin/perl

use strict;
use warnings;

my $name = 'd:/home/data.txt';

open my $file, '<', $name
or die "Cannot open $name: $!";

<$file>; # assuming first line is header, so skip it

my $ps;

while(<$file>) {
chomp;
s/^\s*//g; # skip lines with whitespace only
next unless length $_;
my ($uid, $pid, $ppid, $tty, $stime, $command) = split;

$ps->{$pid} = {
uid => $uid,
ppid => $ppid,
tty => $tty,
stime => $stime,
command => $command,
};
}

use Data:umper;
print Dumper $ps;
__END__

Output:

D:\Home> perl t.pl
$VAR1 = {
'565087' => {
'uid' => 'hbb1',
'ppid' => '1',
'command' => '/usr/bin/PS',
'stime' => '16:09:52',
'tty' => 'con'
},
'582167' => {
'uid' => 'hbb1',
'ppid' => '1',
'command' => '/usr/bin/BASH',
'stime' => '16:09:39',
'tty' => 'con'
}
};
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-12-2004
A. Sinan Unur <(E-Mail Removed)> wrote:

> I think it was Tad who put it most eloquently: You can't just make s**t up
> and expect it to work!



Nope, that was MJD, not me:

http://perl.plover.com/Questions4.html


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-12-2004
Tad McClellan <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> A. Sinan Unur <(E-Mail Removed)> wrote:
>
>> I think it was Tad who put it most eloquently: You can't just make
>> s**t up and expect it to work!

>
>
> Nope, that was MJD, not me:
>
> http://perl.plover.com/Questions4.html


Ah! Apologies for the misattribution and thank you for the link.

Sinan.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-13-2004
A. Sinan Unur <(E-Mail Removed)> wrote in comp.lang.perl.misc:

> I think it was Tad who put it most eloquently: You can't just make s**t up
> and expect it to work!


I've always heard it attributed to MJD, though the quote as I know it
also contains the word "retardo".

Anno
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-13-2004
Anno Siegel <(E-Mail Removed)-berlin.de> wrote:
> A. Sinan Unur <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>> I think it was Tad who put it most eloquently: You can't just make s**t up
>> and expect it to work!

>
> I've always heard it attributed to MJD, though the quote as I know it
> also contains the word "retardo".



That's how I found the link I posted, I googled for "retardo perl".




--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      10-13-2004
Tad McClellan <(E-Mail Removed)> wrote:

> Anno Siegel <(E-Mail Removed)-berlin.de> wrote:
>> A. Sinan Unur <(E-Mail Removed)> wrote in
>> comp.lang.perl.misc:
>>
>>> I think it was Tad who put it most eloquently: You can't just
>>> make s**t up and expect it to work!

>>
>> I've always heard it attributed to MJD, though the quote as I
>> know it also contains the word "retardo".

>
>
> That's how I found the link I posted, I googled for "retardo
> perl".
>
>


I remembered the phrase, "The error mesage is GOD", seeing a clip of
MJD *screaming* it, and that he had posted his list of Good Advice to
usenet. From the Google usenet archive:

http://groups.google.com/groups?selm...1%40plover.com

"Of course it doesn't work! That's because you don't know what you
are doing!"

(Sounds like some of my programs.)
 
Reply With Quote
 
krakle
Guest
Posts: n/a
 
      10-13-2004
(E-Mail Removed) (DeveloperGuy) wrote in message news:<(E-Mail Removed). com>...
> I am very very new to Perl and am trying automate a process in my AIX
> Unix box. I issed the command ps -aef and sent it to a file. How do
> I get how many different users running programs, the total time for
> each user in hours:minutes format, and who is running the longest
> process and the program name? I am not familiar with using the loops.
> I know that I can probably use the date command to specify the date.
> This is where I am stuck thus far. Please help anyone...
>
> #! /usr/bin/perl
>
> use strict;
> use warnings;
>
> @users;
> @tmpfile = OPEN(DataFileHandle, /home/smallp/data.txt);
>
> $tmpfile[0];
> $users[0];
>
> for ($count= 0; $count <= $#users; $count++ {
> If $tmpline[0] eq $users[i]
> if TRUE then exit
> push(@users, $users[0]);
> }


This isn't Perl this is ****... You must NOT of read anything at
all... Pick up a book or read a tutorial or the manpages.. I'm not
even going to rewrite this crap...
 
Reply With Quote
 
DeveloperGuy
Guest
Posts: n/a
 
      10-14-2004
(E-Mail Removed) (krakle) wrote in message news:<(E-Mail Removed). com>...
> (E-Mail Removed) (DeveloperGuy) wrote in message news:<(E-Mail Removed). com>...
> > I am very very new to Perl and am trying automate a process in my AIX
> > Unix box. I issed the command ps -aef and sent it to a file. How do
> > I get how many different users running programs, the total time for
> > each user in hours:minutes format, and who is running the longest
> > process and the program name? I am not familiar with using the loops.
> > I know that I can probably use the date command to specify the date.
> > This is where I am stuck thus far. Please help anyone...
> >
> > #! /usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > @users;
> > @tmpfile = OPEN(DataFileHandle, /home/smallp/data.txt);
> >
> > $tmpfile[0];
> > $users[0];
> >
> > for ($count= 0; $count <= $#users; $count++ {
> > If $tmpline[0] eq $users[i]
> > if TRUE then exit
> > push(@users, $users[0]);
> > }

>



Okay, I finally got the script to read the file.

#! /usr/bin/perl
#This program reads a file based on the command ps -aef called data.txt
#and the following questions have to be answered
#how many users are running programs?
#what is the total time used by each user in hours: minutes?
#who is running the longest process, list user, time and name of program
#use strict;
#use warnings;

open(DataFileHandle, "/home/smallp/data.txt");

@tmpfile = <DataFileHandle>;
@users;
$users[0];

foreach $i (@tmpfile)
{
print "$i\n";
}
close DataFileHandle;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ActiveState Perl and MinGW [was: Perl 5.14 Windows Strawberry Perl 64 bits] Dilbert Perl Misc 0 11-10-2011 02:20 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments