in reply to while loop not working

Could you describe what it is that you are trying to do?

It looks like you are simply reading everything from one file and then appending it to another? Going by the output filename this is just a test, but if that is your intention , then an OS command would achieve this much more easily.

With regard to the body of the if statement, you are only reading one line after the section header. Is this your intent?

And if the intent of this line

($final_gset[$i]), $tmps;

is to assign the line you read in, into an element of the array, then where is the assignment? Ie. '='.

Basically, theere is no need to assign to a temporary var and then to the array, you might as well do this directly

if (/POLYMORPH/) $final_gset[$i] = <IN>; }

If that is your intent, then the other problem with your code is that you are never incrementing $i, so you are always over the same element. Except, that you have never assigned a value to $i, so it has the value undef, which is illegal as an array subscript.

If you had

use strict; use warnings;

at the top of your program, Perl would have probably told you most of this.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Replies are listed 'Best First'.
Re: Re: while loop not working
by harry34 (Sexton) on Jul 15, 2003 at 11:36 UTC
    basically i'm trying to loop so each time POLYMORPH is seen the data below is read and outputed to a txt file. This code does not seem to read or print all the data. i'ts printing out some of the data from each polymorph ?. is there something wrong ?
    my $i; my @final_gset = (); LOOP: while (<IN>) { if (/POLY/) { $final_gset[$i] = <IN>; $i++; next LOOP; } } close (IN); open (TEXT, ">>test.txt") or die "Can't create test.txt: $!\n"; foreach $_(@final_gset) { print TEXT "$_"; } close TEXT;

      The problem is that you seem to be aware that

      while( <IN> ) {...

      reads a single line, hence you need to loop back to read the next one, but then when you get to

      if(/POLY/) { $final_gset[$i] = <IN>; ...

      you seem to be expecting that line to somehow magically read multiple lines. It won't.

      I might go into a more detailed explanation, but I still think that you don't need to write this program. You are (trying to ) read all the lines from one file and then append them to another. This would be much more easily accomplished using

      C:\> type graph_set.txt >>test.txt

      or$ cat graph_set.txt >>test.txt

      depending which view of the world you see as being rightous:)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

        Almost, but not exactly, Browser

        He wants all lines after POLYMORPH.

        So grep -v POLYMORPH graph_set.txt >>test.txt might be an option.

      is there something wrong ?

      Yes. You say that you want the data below POLYMORPH. Your code does not do that.

      Perhaps an alternative approach is to load the contents of your input file into a scalar, split the scalar on 'POLYMORPH', and then if it is neccessary to tweak it further perform some regexes on the resulting list, before dumping it to your output file.

      I hope this helps