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

I know this a very easy question for the monks, but I can't get it. I have the following:
#!/usr/bin/perl -w $file = "in.txt"; open (FILE, "$file"); my @results; while (<FILE>) { if (/^N\d+\s+\*(.*)/) { push @results, "!$1"; } else { push @results, /[ABC]([-+]*[\d\.]+)/g; } } print join ' ',@results;
I figured out open (OUT, ">out.txt") but I'm stuck on how to pass the data into the file.Thank you for answering such a rookie question...

Replies are listed 'Best First'.
Re: Creating new file
by dws (Chancellor) on Jul 23, 2002 at 23:21 UTC
    I figured out open (OUT, ">out.txt") but I'm stuck on how to pass the data into the file.

    print OUT "@results\n"; close(OUT);

    You don't need to use a join() to put spaces between the elements in @results. Array interpolation will do that for you.

Re: Creating new file
by DamnDirtyApe (Curate) on Jul 24, 2002 at 05:29 UTC

    To expand on dws's answer, the line open( OUT, ">out.txt" ) creates a filehandle called OUT that refers to the write-only access to out.txt.

    Normally, the print command will use STDOUT as the default file handle. Hence, the following two lines will typically do the same thing:

    print "Hello, World!\n" ; print STDOUT "Hello, World!\n" ;

    To print to a file instead of STDOUT, simply change the filehandle passed to the print statement.

    print FILE "Hello, World!\n" ;

    Also, clean up behind yourself; ie. don't forget to close your filehandle when you're done (it probably won't break your program if you don't, but it's good practice.)

    close FILE ;

    For more info, see the docs on open and print.


    _______________
    D a m n D i r t y A p e
    Home Node | Email