srkaeppler has asked for the wisdom of the Perl Monks concerning the following question:

Hello all- I come again with a question about some trouble i am having with file handles and running two different loops

so here is the code:

print "What is input file name?: "; chomp($input = <STDIN>); print "What is output file name: "; chomp($output = <STDIN>); open (INPUT, "$input") || die "died opening input\n";#opening up ngc18 +8cata and putting it into INPUT# open (STDOUT, ">$output") || die "died opening output\n"; #opening up +ngc188cat2 and setting output of out# $cfo = "O"; $status = "STATUS=OK"; $m = "^M"; open (INTER, ">>fred"); while ($inline = <INPUT>) { #INPUT defined as variable# @mag = split(/\s+/, $inline); #firstline of array defined by split of + white space into variable inline# ############################################# if ($mag[12] >= 12 && $mag[12] <= 20) { print INTER "$inline"; #printing to INTER } } while ($sep = <INTER>) { @firstline = split(/\s+/, $sep); $hms1 = $firstline[2]; $rah2 = ($hms1 / 15); $rah3 = int($rah2); $ram1 = ( abs($rah2 - $rah3) )* 60; $ram2 = int($ram1); $ras1 = abs($ram1 - $ram2) * 60; $ras2 = sprintf "%-02.3f", $ras1 ; $hms2 = $firstline[3]; $hh2 = sprintf "%1d", ($hms2); $mm2 = sprintf "%02d", (60*( abs($hms2) - abs($hh2) ) ); $ss2 = sprintf "%-02.2f", 3600*abs($hms2) - 3600*abs($hh2) - 60*abs($ +mm2); $obid = "NGC 188 - $firstline[1]"; write; #writing to STDOUT } close INPUT;
The rest of the code after this point is working fine, so i am not going to bother you all with it. In the first "if" loop, i am trying to take information from with in the dataset i was given, and I want this dataset to be printed only if a certain column(the 12th in this case) has values between say 12 and 16. If it does, then I want to run that new dataset(contain the true values) through the rest of the program. If it does not, then i dont really care where it goes. I hope this makes sense.

So I am not sure if my control structure is any good or not, or if this can all be done in one loop rather than in two loops. So i am a little bit confused about opening up a filehandle here... i understand the input and output ones above, but opening an "intermediate one" (like INTER) is confusing me. I figured that might have been the easiest way. So my question would be, can this all be done in one loop rather than two loops? Is my "if" loop wrong? Did i open the INTER filehandle properly? did i print properly? and any other general comments or critisms(including my poor english )

Thank you very much! Steve

Replies are listed 'Best First'.
Re: Running two loops
by davorg (Chancellor) on Sep 01, 2004 at 16:15 UTC

    I'm not really sure what you're trying to do, but I see one obvious problem - you're opening INTER in write mode and then trying to read from it.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Running two loops
by Eimi Metamorphoumai (Deacon) on Sep 01, 2004 at 16:24 UTC
    Unless I'm missing something, it sounds like you want to ignore data on certain lines. You don't need two loops and the convoluted structure for that, you just need some control modifiers.
    while ($inline = <INPUT>) { @mag = split(/\s+/, $inline); next unless $mag[12] >= 12 && $mag[12] <= 20; #the rest of your code here, using @mag instead of @firstline; }
    It looks like right now, you're trying to make two passes over your data, the first to decide what to deal with, the second to actually deal with it. It's usually much easier to just skip the data you don't need in a single pass.
Re: Running two loops
by wfsp (Abbot) on Sep 01, 2004 at 16:37 UTC
    It might be easier if print your output to a file rather than use STDOUT. If you
    use strict; use warnings;
    and declare all the variables then you may get some useful information on what is actually happening.

    It would certainly be easier to help you.

Re: Running two loops
by doowah2004 (Monk) on Sep 01, 2004 at 19:59 UTC
    Typically file handles are as follows:

    open (FILEHANDLE,file);
    the file is usually in the format as follows:

    '< filename' Open file for input '+< filename' Open file for input and output append '+> filename' Open file for input and output and truncate the data 'filename' Open file for input '>filename' Open file for output, truncate data '>>filename' Open file for output, append to file


    You are using open (INTER, ">>fred");
    This is strictly for writing, append to file. Because I do not see where you closed the file, I can not assume that you will not try to write to it later, so I would change it to: open (INTER, "+<fred");


    Cameron