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

Hi all, I'm hoping soemone will be able to help me out here. I'm a Perl newbie and I'm using the following code to skip the first 10 lines and any blank lines of an array, then split the individual lines... The problem is that the if ... next code used to skip the first/blank lines does not appear to work. When The code gets to the stage of popping and shifting, it is still working on the first lines of the file. Does anyone know what is causing this? The original code actually came from an O'Reilly book which makes it all the more confusing ! :( Any help would be sincerely appreciated! Cheers in advance, TB34
foreach (@rebasefile) { if (/1 .. 10/) {next;} if (/^\s*$/) {next;} my @fields = split("",$_); $name = shift(@fields); $site = pop(@fields); $regexp = IUB_to_regexp($site); $rebasehash{$name}="$site $regexp"; } return %rebasehash; }

Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Cannot skip first lines in array
by gellyfish (Monsignor) on Oct 20, 2004 at 12:28 UTC

    I don't think you saw exactly that code in any book. You should replace:

    if (/1 .. 10/) {next;}
    with
    if (0 .. 9) {next;}
    Of course that doesn't actually work either because it will only work in the context of a:
    while(<FILEHANDLE>) { # ... }
    type statement.

    /J\

      The flip-flop really should start at 1 and be 1 .. 10 because after the first read, $. will be 1.

      Related documents:
      perlvar - Perl predefined variables
      perlop - Perl operators and precedence

      ihb

      See perltoc if you don't know which perldoc to read!
      Read argumentation in its context!

      because it will only work in the context of a: while(<FILEHANDLE>)

      or, of course:

      { local $. = 0; for (@array) { if ( 0..9 ) {} .... } $.++; }
      =)
      Admittedly, the code was changed slightly from the book version but caused an identical error to what was shown in the book. I've just downloaded the online version of the book code however, and it is actually different from what was shown in the book. As you said, it uses a while loop as opposed to the foreach loop shown in the book.
Re: Cannot skip first lines in array
by zejames (Hermit) on Oct 20, 2004 at 12:46 UTC
    Why not beginning by splicing your array :
    @rebasefile = splice @rebasefile, 10;

    Then you can just do whatever you want on the remaining strings from the array.

    --
    zejames
      Thanks a lot guys! All very useful suggestions and I've got it working now. It would have been nice if the code suggested in the book had actually worked! ;) Cheers again, TB34
Re: Cannot skip first lines in array
by periapt (Hermit) on Oct 20, 2004 at 12:42 UTC
    Assuming that @rebasefile is an array you can skip the first 10 indices in a couple of ways
    foreach (@rebasefile[10..$#rebasefile]){ # skips the first ten automat +ically next if(/^\s*$/); ... }
    # alternately if you don't want the overhead of creating an array slice
    foreach (@rebasefile){
    next for (0..9); # of course, this has overhead of its own
    next if(/^\s*$/);
    ...
    }
    I haven't checked which implementation is faster. I suspect that for just 10 elements, there is a non-measurable difference. If you were ignoring 100 or more elements, the slice is probably more efficient.

    Update:

    Ignore the second loop. Never get smug before your first cup of coffee.

    PJ
    use strict; use warnings; use diagnostics;
      foreach (@rebasefile){ next for (0..9); # of course, this has overhead of its own next if(/^\s*$/); ... }
      That doesn't skip the first 10 elements, it just calls an empty loop 10 times for each element.

      Dave.

      i don't think next for (0..9); is working the way you want.
      however, the first foreach loop should work just fine.