473,543 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<=@oldsession s; $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<=@oldsession s; $i++) {
print "Killing $oldsessions[$i][0], Process ID $oldsessions[$i]
[6]" . "\n";
}

Jul 19 '05 #1
3 2873
Mothra <Mo****@mothra. com> wrote in message news:<Wp******* **************@ news.easynews.c om>...
$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.c om>...
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<=@oldsession s; $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<=@oldsession s; $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
1728
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 quite easy: for (my $i=0.25; $i<=2.25; $i+=0.25) { printf "%9.2f\n", $i;
11
2580
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 VBA code, or automation, in the JET world. Some who have not yet worked with MS-SQL may be interested. The script is indented in reality but I have...
2
1992
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: deduction ID, start date, end date and amount what i want to be able to do is keep adding a deduction each 7 days from the
4
1466
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 index counter and incrementing it. For example, (completely arbitrary), I have two strings of the same length, and I want to return a string which,...
10
4580
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 to increase/decrease min is the minimum value that x can assume max is the maximum value that x can assume
1
1217
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
4749
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 + sizeof(ptr);
3
2231
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 one record in at a time. insert into `mastertable3` select ' ', 0301_2500.ssn, 0301_2500.vsssn, 0301_2500.name_ind, 0301_2500.pay_gr_aa from...
24
2396
by: tanmay | last post by:
please tell me how to get the following output using loops... 1) * * * * * * * * * * * * * * * *
0
7411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7354
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7693
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5282
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3394
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3394
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1824
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.