473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

looping/incrementing problem...

Here's what I'm trying to do (kill off old Unix logins):

---------------------
$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line[5] eq "old");
push @{$oldsessions[$i]}, @line;
$i++;
}
---------------------

Which is fine unless you have strict pragma on, which i always do,
becuase then $i isn't locally defined within the loop. Is there a way I
can combine reading in the contents of $who with an incrementing $i all
within the same scope? Or is there a better way than using $i. Bear in
mind that @oldsessions is an array of annoymous arrays.

Here is the whole code to put it in context...

---------------------
use strict;
use IO::Pipe;

my @oldsessions;
my $who=IO::Pipe->new;
$who->reader('/usr/bin/who', '-u');

$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line[5] eq "old");
push @{$oldsessions[$i]}, @line;
$i++;
}

print "The following sessions are older than 24 hours:\n";

for($i=0; $i<=@oldsessions; $i++) {
print join(' ', @{$oldsessions[$i]}) . "\n";
}

print "Terminate them? (y/N): ";
chomp($a=<STDIN>);
unless($a eq "y"){
print "Exited without killing any sessions.\n";
exit(0);
}

$i=0;
for($i=0; $i<=@oldsessions; $i++) {
print "Killing $oldsessions[$i][0], Process ID $oldsessions[$i]
[6]" . "\n";
}

Jul 19 '05 #1
3 2867
Mothra <Mo****@mothra.com> wrote in message news:<Wp*********************@news.easynews.com>.. .
$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line[5] eq "old");
push @{$oldsessions[$i]}, @line;
$i++;
}
[snip]
Is there a way I can combine reading in the contents of $who with
an incrementing $i all within the same scope? Or is there a better
way than using $i.
You could also use Perl's input line number variable, $., instead of
manually incrementing $i:

while (<$who>) {
chomp;
my @line = split;
next unless $line[5] eq 'old';
$oldsessions[$.] = \@line;
}
Bear in mind that @oldsessions is an array of annoymous arrays.
Sure thing. FWIW, I was actually a little confused about the way you
added to @oldsessions:
push @{$oldsessions[$i]}, @line;


I personally don't like using 'push' when a simple assignment would
do:

@{ $oldsessions[$.] } = @line;

You could even do something that looks cleaner, IMHO:

$oldsessions[$.] = \@line;

Cheers,
David
Jul 19 '05 #2
Mothra wrote:

$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line[5] eq "old");
push @{$oldsessions[$i]}, @line;
$i++;
}
<snip>
Is there a way I can combine reading in the contents of $who with
an incrementing $i all within the same scope?


Do you mean like this:

push @{$oldsessions[$i++]}, @line;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #3
axs
{
$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line[5] eq "old");
push @{$oldsessions[$i]}, @line;
$i++;
}
}

Mothra <Mo****@mothra.com> wrote in message news:<Wp*********************@news.easynews.com>.. .
Here's what I'm trying to do (kill off old Unix logins):

---------------------
$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line[5] eq "old");
push @{$oldsessions[$i]}, @line;
$i++;
}
---------------------

Which is fine unless you have strict pragma on, which i always do,
becuase then $i isn't locally defined within the loop. Is there a way I
can combine reading in the contents of $who with an incrementing $i all
within the same scope? Or is there a better way than using $i. Bear in
mind that @oldsessions is an array of annoymous arrays.

Here is the whole code to put it in context...

---------------------
use strict;
use IO::Pipe;

my @oldsessions;
my $who=IO::Pipe->new;
$who->reader('/usr/bin/who', '-u');

$i=0;
while (<$who>) {
chomp($_);
my @line = split(/\s+/, $_); # Split it into an array
next unless ($line[5] eq "old");
push @{$oldsessions[$i]}, @line;
$i++;
}

print "The following sessions are older than 24 hours:\n";

for($i=0; $i<=@oldsessions; $i++) {
print join(' ', @{$oldsessions[$i]}) . "\n";
}

print "Terminate them? (y/N): ";
chomp($a=<STDIN>);
unless($a eq "y"){
print "Exited without killing any sessions.\n";
exit(0);
}

$i=0;
for($i=0; $i<=@oldsessions; $i++) {
print "Killing $oldsessions[$i][0], Process ID $oldsessions[$i]
[6]" . "\n";
}

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: drife | last post by:
Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? In Perl this construct...
11
by: Lyle Fairfield | last post by:
The stored procedure script below is an example of how looping, case statements and output parameters can be used in MS-SQL stored procedures to accomplish things for which we may have had to use...
2
by: ozgirl via AccessMonster.com | last post by:
Hi, hopeing someone can help me here... I have a deductions table and form and want to be able to add many deductions to the table by having only a few fields on the form form fields:...
4
by: Iain King | last post by:
When I loop over one list I use: for item in items: print item but often I want to loop through two lists at once, and I've been doing this like I would in any other language - creating an...
10
by: pozz | last post by:
Hi all, I need to write a simple incrementing/decrementing function like this: unsigned char change( unsigned char x, unsigned char min, unsigned char max, signed char d); x is the value...
1
by: Dave128 | last post by:
I am trying to write a program for an assignment that uses a For loop to produce an output that looks like this: * ********** ********** * ** ...
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
3
by: DougieC | last post by:
I am trying to insert records into a mastertable. The first field is auto-incrementing and therefore I am unable to insert all records at the same time. I have create the following code to insert...
24
by: tanmay | last post by:
please tell me how to get the following output using loops... 1) * * * * * * * * * * * * * * * *
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.