Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Why is there only one column in the output file?

by perllearner007 (Acolyte)
on Apr 05, 2012 at 16:43 UTC ( [id://963711]=perlquestion: print w/replies, xml ) Need Help??

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

Hi there PerlMonks, Here is my code:
#!usr/bin/perl -w use strict; # open the file that has to be read my $input = "Users/Dektop/file123.txt"; die "Cannot open $input\n" unless (open(IN, $input)); #open a new file which has to be made open (my $out, ">outfile123.txt"); #In the while loop, put the columns that have to be printed in the new + file. while(<IN>){ my @fields = split /\t/,$_; my $ID = $fields[0]; my $WEIGHTED_SUM = $fields[9]; #print the columns { print $out "$ID\ $WEIGHTED_SUM \n"; } } #close the filehandles close($input); close($out);

The output file is extracting the ID column but for the WEIGHTED_SUM , I get the following error, Use of uninitialized value $WEIGHTED_SUM in concatenation (.) or string at sample1.pl line 20, <IN> line 2148788.

Can some tell me what is going on and why I am not able to extract both the columns? Is there a way to fix it?

Replies are listed 'Best First'.
Re: Why is there only one column in the output file?
by toolic (Bishop) on Apr 05, 2012 at 16:55 UTC
    One likely reason for the warning message is that line 2148788 of your input file has fewer than 10 tab-separated columns. $fields[9] is probably undefined.

    You could open your input file in an editor, go to line 2148788, and check if there are really 10 columns. Or, something in the Basic debugging checklist could help:

    warn scalar(@fields), " $.\n";

    Updated with code example.

      Thank you so much it is sorted!
Re: Why is there only one column in the output file?
by ryber (Acolyte) on Apr 05, 2012 at 17:01 UTC

    toolic is correct.

    you probably want something like this

    while(<IN>){ my $line = $_; unless ($line =~ /\S/) { print STDERR "Skipping line $. (blank)\n"; next; } my @fields = split /\t/,$line; unless (@fields >= 10) { print STDERR "Error: line $.: less than 10 fields: $_\n"; } . . .
      you probably want something like this

      while(<IN>){
          my $line = $_;

      No.    Nobody wants something like that.

      Why store the same value in two different variables when you only use one variable inside the loop.

      Perhaps you meant:

      while ( my $line = <IN> ) {

        jwkrahn, you miss the point.

        The main point was to check for and weed out blank lines and those which do not contain enough fields. I agree it would be better to assign the $line var in the while statement as well; I wrote it that way to change the OP's code as little as possible and focus on the input validation pieces.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-18 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found