looping/incrementing problem...

Discussion in 'Perl' started by Mothra, Dec 9, 2003.

  1. Mothra

    Mothra Guest

    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";
    }
    Mothra, Dec 9, 2003
    #1
    1. Advertising

  2. Mothra

    David Guest

    Mothra <> wrote in message news:<WplBb.2619157$>...

    > $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
    David, Dec 9, 2003
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Dec 9, 2003
    #3
  4. Mothra

    axs Guest

    {
    $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 <> wrote in message news:<WplBb.2619157$>...
    > 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";
    > }
    axs, Dec 10, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josh Graham
    Replies:
    3
    Views:
    23,159
    Nicolas Matringe
    Apr 7, 2004
  2. Salman

    Incrementing value test

    Salman, Apr 13, 2005, in forum: VHDL
    Replies:
    2
    Views:
    615
    Engineering Guy
    Apr 13, 2005
  3. Ron
    Replies:
    0
    Views:
    304
  4. Gary

    problem incrementing my variable

    Gary, Jan 27, 2009, in forum: C Programming
    Replies:
    43
    Views:
    924
  5. Replies:
    5
    Views:
    261
Loading...

Share This Page