looping/incrementing problem...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mothra

    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";
    }

  • David

    #2
    Re: looping/incrementing problem...

    Mothra <Mothra@mothra. com> wrote in message news:<WplBb.261 9157$Id.399048@ news.easynews.c om>...
    [color=blue]
    > $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++;
    > }[/color]

    [snip]
    [color=blue]
    > 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.[/color]

    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;
    }
    [color=blue]
    > Bear in mind that @oldsessions is an array of annoymous arrays.[/color]

    Sure thing. FWIW, I was actually a little confused about the way you
    added to @oldsessions:
    [color=blue]
    > push @{$oldsessions[$i]}, @line;[/color]

    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

    Comment

    • Gunnar Hjalmarsson

      #3
      Re: looping/incrementing problem...

      Mothra wrote:[color=blue]
      >
      > $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++;
      > }[/color]

      <snip>
      [color=blue]
      > Is there a way I can combine reading in the contents of $who with
      > an incrementing $i all within the same scope?[/color]

      Do you mean like this:

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

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

      Comment

      • axs

        #4
        Re: looping/incrementing problem...

        {
        $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 <Mothra@mothra. com> wrote in message news:<WplBb.261 9157$Id.399048@ news.easynews.c om>...[color=blue]
        > 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";
        > }[/color]

        Comment

        Working...