sunil9009 has asked for the wisdom of the Perl Monks concerning the following question:

Can I have some simple solution. Something like this ? Its not working though. Dont know why

#!/usr/bin/perl -w use strict; use warnings; open(my $fh, '<', '/tmp/list') or die "Unable to open file, $!"; my @entire_file=<$fh>; for my $i (0..$#entire_file) { my @group; push @group,$entire_file[$i] until ( $i % 5 == 0 ); `tool.pl @group`; }

Replies are listed 'Best First'.
Re: Can I have some simple solution
by davido (Cardinal) on Apr 11, 2014 at 04:18 UTC

    until is a looping construct. The entire loop is this:

    push @group, $entire_file[$i] until ( $i % 5 == 0 );

    Which can also be written as this:

    while( $i % 5 != 0 ) { push @group, $entire_file[$i]; }

    As you can see (or should be able to see if you've spent any time at all programming), $i never changes in this loop. ...so it will either not execute at all (if $i is a multiple of 5), or will execute forever otherwise. Well, "forever", until you fill your memory with copies of $entire_file[$i].

    You might have intended something more along the line of:

    push @group, $entire_file[$i] unless $i % 5 == 0;

    That's not a looping construct -- it's just a conditional. In this way, $i will have its value change as the outer foreach loop progresses.


    Dave

Re: Can I have some simple solution
by boftx (Deacon) on Apr 11, 2014 at 01:36 UTC

    This might have made more sense had the posting date been "April 1" instead of "April 10".

    That being said, the root of any problem that might be present lies in the initial values of the parameters presented to the modulus operator. If I may paraphrase Asimov, the solution is left as an exercise for the OP.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Can I have some simple solution
by DrHyde (Prior) on Apr 11, 2014 at 10:35 UTC

    I assure you that it is working. It's doing exactly what you told it to. Perhaps you should consider telling us what you *expected* it to do, and how what it does differs from that.

Re: Can I have some simple solution
by sunil9009 (Acolyte) on Apr 11, 2014 at 05:45 UTC

    Sorry for spamming here, it was my copy/paste mistake in multiple windows. Its related to http://www.perlmonks.org/?node_id=1081756 Please close this post