Loading ...
Sorry, an error occurred while loading the content.
 

Re: nested "while"s...(from newbie)

Expand Messages
  • Charles K. Clarkson
    ... After the inner loop iterates once, it is at the end of the file. When the color changes in the outer loop to yellow, the inner loop doesn t run because we
    Message 1 of 2 , Jan 4, 2001
      alexrrr@... asked:
      : Hi, this i my very fist posting to this group, i
      : have a simple question. I've got two small files
      :
      : color1 color2
      :
      : blue apple
      : yellow pear
      : red banana
      :
      : i want a simple program that opens both files and
      : prints out contents like this
      : blue
      : apple
      : blue
      : pear
      : blue
      : banana
      : yellow
      : apple
      : yellow
      : pear.... and so on.
      :
      : i have problem with the nested whiles which i am
      : trying to use
      :
      : while ($dex=<FEDEX>) #opening the file
      : {
      : while ($tai=<UPS>)
      : {print $dex;
      : print $tai; }
      : }

      After the inner loop iterates once, it is at the end of
      the file. When the color changes in the outer loop to
      yellow, the inner loop doesn't run because we haven't
      reset the file to the beginning.


      :all i get something like this
      :
      : blue
      : apple
      : blue
      : pear
      : blue
      : banana
      :
      : It never goes on to the "yellow" which is the second
      : line in the color1 file!!!! what am i doing wrong?? HELP!
      :
      : NOTE: FEDEX is filehandle for file color1 and UPS is
      : filehandle for file color2.
      :
      : Thank you VERY VERY MUCH!!!1
      :
      : I've not being getting any sleep because of this!!!

      This is not the fastest or shortest solution, but it easy
      to understand:

      my @tai = <UPS>; # each line goes into a
      my @dex = <FEDEX>; # separate array element

      foreach my $dex (@dex) {
      next if $dex =~ /^\s+$/; # skip blank lines
      chomp $dex; # get rid of \n
      foreach my $tai (@tai) {
      next if $tai =~ /^\s+$/; # skip blank lines
      chomp $tai; # get rid of \n
      print "$dex\n$tai\n";
      }
      }

      HTH,
      Charles K. Clarkson

      Now - go get some sleep . . .
    Your message has been successfully submitted and would be delivered to recipients shortly.