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

Re: [PBML] nested "while"s...(from newbie)

Expand Messages
  • Andrew Johnson
    ! while ($dex= ) #opening the file ! { ! while ($tai= ! {print $dex; ! print $tai; }} !
    Message 1 of 2 , Jan 3 9:32 PM
      ! while ($dex=<FEDEX>) #opening the file
      ! {
      ! while ($tai=<UPS>
      ! {print $dex;
      ! print $tai; }}
      !
      [snip]
      ! It never goes on to the "yellow" which is the second line in the
      ! color1 file!!!! what am i doing wrong?? HELP!

      It does go on the second (and third) line of FEDEX -- but since
      during the first loop we looped all the way to the end of the
      UPS file, the inner loop has nothing left to read and doesn't
      execute (and thats where the print statements are, so nothing
      further gets printed).

      You need to either: use seek() after the inner loop to seek back
      to the beginning of the UPS file (so it can be read again):

      open(FEDEX,'color1') || die "Can't open file: $!";
      open(UPS, 'color2') || die "Can't open file: $!";
      while(my $dex = <FEDEX>){
      while(my $tai = <UPS>){
      print $dex;
      print $tai;
      }
      seek UPS, 0, 0;
      }
      close FEDEX;
      close UPS;

      or, read both files into arrays (if they aren't too large) and
      use nested foreach loops over those arrays:

      open(FEDEX,'color1') || die "Can't open file: $!";
      open(UPS, 'color2') || die "Can't open file: $!";
      my @dex = <FEDEX>;
      my @tai = <UPS>;
      close FEDEX;
      close UPS;
      foreach my $dex (@dex) {
      foreach my $tai (@tai) {
      print $dex;
      print $tai;
      }
      }

      Hope it helps,
      andrew

      --
      Andrew L. Johnson http://members.home.net/andrew-johnson/
    • alexrrr@hotmail.com
      ... i ve just woke up from a great sleep!!1 thanks for the help! Thank you so much guys... I knew i had to be something simple!! It is great that people like
      Message 2 of 2 , Jan 4 1:41 PM
        --- In perl-beginner@egroups.com, Andrew Johnson <andrew-
        johnson@h...> wrote:
        > ! while ($dex=<FEDEX>) #opening the file
        > ! {
        > ! while ($tai=<UPS>
        > ! {print $dex;
        > ! print $tai; }}
        > !
        > [snip]
        > ! It never goes on to the "yellow" which is the second line in the
        > ! color1 file!!!! what am i doing wrong?? HELP!
        >
        > It does go on the second (and third) line of FEDEX -- but since
        > during the first loop we looped all the way to the end of the
        > UPS file, the inner loop has nothing left to read and doesn't
        > execute (and thats where the print statements are, so nothing
        > further gets printed).
        >
        > You need to either: use seek() after the inner loop to seek back
        > to the beginning of the UPS file (so it can be read again):
        >
        > open(FEDEX,'color1') || die "Can't open file: $!";
        > open(UPS, 'color2') || die "Can't open file: $!";
        > while(my $dex = <FEDEX>){
        > while(my $tai = <UPS>){
        > print $dex;
        > print $tai;
        > }
        > seek UPS, 0, 0;
        > }
        > close FEDEX;
        > close UPS;
        >
        > or, read both files into arrays (if they aren't too large) and
        > use nested foreach loops over those arrays:
        >
        > open(FEDEX,'color1') || die "Can't open file: $!";
        > open(UPS, 'color2') || die "Can't open file: $!";
        > my @dex = <FEDEX>;
        > my @tai = <UPS>;
        > close FEDEX;
        > close UPS;
        > foreach my $dex (@dex) {
        > foreach my $tai (@tai) {
        > print $dex;
        > print $tai;
        > }
        > }
        >
        > Hope it helps,
        > andrew
        >
        > --
        > Andrew L. Johnson http://members.home.net/andrew-johnson/

        i've just woke up from a great sleep!!1 thanks for the help!

        Thank you so much guys... I knew i had to be something simple!! It is
        great that people like you form this newgroup! you make this work!
        Hope someday, when i've already learned my "seek" and "foreach" well,
        i get the chance to help someone.

        Andrew... can you explain a little bit more how that "seek"
        command works??? Thank you!


        Once again thank you!!

        Alejandro.
      Your message has been successfully submitted and would be delivered to recipients shortly.