William M West wrote:
>
> in the post that i am now replying to, i said that i couldn't get
> the program to skip lines as it parsed through the output of another
> program.
>
> i have "solved" the problem- but i do not know WHY this works the
> way it does.
>
> <SNIP>
>
>
>
> this worked great :)  (thanks to drieux) - in fact, it would let
> me have several programs input to this one at once :) (if i'm not mistaken)

Get just one working, for now :)

[snip]

> >
> >sub skip_lines {
> > my $lines = shift; #number of lines in file to skip over
> >
> > print "in skip_lines() $_ before skip\n"; #debug
> >
> > #<CMD> for 1..$lines; #not functioning...
> >
> > for (0..$lines){
> > my $trash = <CMD>;
> > print "trash is $trash \n"; #debug
> > }
>
>
> the fix was to replace the above for loop with the following::
>
> for my $trash (0..$lines) {
> $_ = <CMD>;
> print "the line skipped is $_\n"; #debug
> }
>
> > print "in skip_lines() $_ after skip\n"; #debug
> >
> >}
>
>
> well- i don't understand it.  WHY does this work?
>
> if someone could clarify the mechanics behind this behaviour,
> i would be very grateful!

Hi Willy.

It's because $_ wasn't being assigned. Your original calls to
skip_lines read like this:

  skip_lines ($skip->[0]);
  my $partnumber = $_;

  skip_lines ($skip->[1]);
  my $inventory = $_;

but you were reading your record into $trash instead. It's a bad
idea to use $_ as a global variable. It's meant to be used invisibly
and in very close scope in things like:

  while (<>) {
    s/this/that/g;
    print;
  }

and not for passing stuff out of subroutine calls. If you're not sure
then just don't explicitly mention '$_' at all and you should be safe.

I think you have an over-elaborate design for this anyway. I think it's
likely that your data can be processed better than just by skipping 'n'
lines between useful records. And even if this is the way you want to go
there's nothing wrong with

  <CMD> for 1 .. $skip->[0]
  my $partnumber = <CMD>;

  <CMD> for 1 .. $skip->[1]
  my $inventory = <CMD>;

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to