in reply to Re: while loop not working
in thread while loop not working

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;

Replies are listed 'Best First'.
Re: Re: Re: while loop not working
by BrowserUk (Patriarch) on Jul 15, 2003 at 13:14 UTC

    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.

        Possibly true. I was hoping to pursuade Harry to elusidate a little upon his requirements so that we might offer a solution that fits his problem. I suspect that test.txt is simply that. A mechanism to test whether this part of the code is working rather than the final requirement.

        Which ever interpretation you place on the code as is, there are better ways of achieving the goal. If he would tell us what he wants, it would be easier to suggest something.


        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

Re: Re: Re: while loop not working
by bm (Hermit) on Jul 15, 2003 at 11:50 UTC
    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