Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

no output or error received

by rocketperl (Acolyte)
on Jun 28, 2013 at 09:30 UTC ( #1041200=perlquestion: print w/ replies, xml ) Need Help??
rocketperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi i was trying to execute a simple perl script where i opened a TSV document and wanted to paste the first column only in a new file. But iam not getting any errors or output with this. I have created an empty document for my output file as well. Thanks.
Below is the code i used. #!/usr/bin/perl no warnings "all"; open (FILE, 'G0_Probe_Report.tsv'); while (<FILE>) { chomp; ($name, $ID, $chromosome, $start_location, $control_type, $IP_fore +ground, $IP_background, $WCE_foreground, $WCE_background, $IP_backgro +und_SD, $WCE_background_SD,$norm_IP_signal, $norm_WCE_signal, $normal +ized_log_ratio, $x, $p_value, $z, $xbar, $p_xbar, $in_bound_regoin, $ +exclude, $primary_annotation, $secondary_annotation, $accession_numbe +r, $primary_annotation_type) = split("\t"); open (OFILE, '>name.tsv'); print "$name\n OFILE"; } close (FILE); close (OFILE); exit;

Comment on no output or error received
Download Code
Re: no output or error received
by marto (Chancellor) on Jun 28, 2013 at 09:49 UTC

      Just to be a little more explicit, you don't want to reopen the file every iteration of your loop. There's no reason to reopen it each time you're about to print. So open it before you start reading the input and writing it out.

Re: no output or error received
by Athanasius (Monsignor) on Jun 28, 2013 at 09:55 UTC

    Hello rocketperl,

    You are opening the filehandle OFILE for writing on each loop iteration. That truncates the file every time! You need to open OFILE once only, before the while loop starts, and then print to it as marto has shown.

    Update: As ww points out, it might be clearer to say that when you (re-)open an existing file for writing, you will then be overwriting whatever was there before. Although open uses the term “truncate” in this context, this should not be confused with Perl’s truncate function which reduces a file to a given length.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: no output or error received
by kcott (Abbot) on Jun 28, 2013 at 09:58 UTC

    G'day rocketperl,

    "But iam not getting any errors or output with this."

    That's not overly surprising as you've turned off all warnings with:

    no warnings "all";

    Also, you haven't used strict (which would have provided some output) and you're not checking whether you're IO is working. I'd recommend you start your script like this:

    #!/usr/bin/perl use strict; use warnings;

    Next take a look at open. See how the two examples at the start of the page end in:

    ... or die "cannot open ...: $!";

    This gives feedback if there were problems opening the file. Also note the 3-argument form with my $fh which is preferable to the 2-argument form you're currently using. An alternative to hand-crafting these messages for each open is to use autodie.

    Once you make these changes to your code, you'll get some feedback. You may be able to fix the problems yourself. If you don't understand the messages, you can get more verbose output with diagnostics.

    If, after all that, you're still stuck, post again with the output you get and I'm sure someone will be able to help.

    -- Ken

      Thanks so much. It worked and got my output!
Re: no output or error received
by ww (Bishop) on Jun 28, 2013 at 10:07 UTC
    You were close...

    Slightly abbreviated; syntax and logic problems fixed... and it works for me.

    #!/usr/bin/perl use 5.016; use strict; use warnings; # no warnings "all"; # WHY? warnings are to help you get a compilab +le program # 1041200 open (OFILE, '>', 'name.tsv') or die "Can't open for output, $!"; # C +HECK opens while (<DATA>) { chomp; my ($name, $ID, $chromosome, $start_location, $control_type) = spl +it("\t"); print "$name \n"; # open (OFILE, '>name.tsv'); # OPENING HERE OVERWRITES PREVIOUS +ENTRY EACH TIME THRU print OFILE "$name\n"; # HANDLE and LIST WERE REVERSED } __DATA__ fred 123 GeeWhiz 00 freak jack 4321 Whoosh 04 paternalist sam 9876 imme 27 passive

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041200]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-09-01 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (14 votes), past polls