473,799 Members | 3,209 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 2888
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
1743
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
2628
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 aligned it all left to make reading easier, (I hope). The Sproc takes an Account ID, an...
2
2003
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
1489
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, at each character
10
4608
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
1227
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
4825
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
2257
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 time_period, Mastertable3 right join 0301_2500 on mastertable3.ssn = 0301_2500.SSN where ...
24
2426
by: tanmay | last post by:
please tell me how to get the following output using loops... 1) * * * * * * * * * * * * * * * *
0
9687
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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.