Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Deleting a columnheader

by bluray (Sexton)
on Oct 07, 2011 at 16:21 UTC ( #930211=perlquestion: print w/replies, xml ) Need Help??
bluray has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I am trying to get rid of the last column in the output file. Though, I was successful in getting rid of the column, the columnheader still is present. I tried different things, but, I wasn't able to get the desired output. I am using three input files. The output file will contain the matching entries from the first column of the input files. The header for output file is the same as the input files except the last column. All the input files have 6 columns (so, my output file should have only 5 columns).

#!/usr/bin/perl -w use strict; use warnings; use Text::CSV_XS; open (my $FILE1, '<', "fileX.csv") or die "cannot open file1 $!\n"; open (my $FILE2, '<', "fileY.csv") or die "cannot open file2 $!\n"; open (my $FILE3, '<', "fileZ.csv") or die "cannot open file3 $!\n"; open (my $FILE4, '>', "Outputfile.csv") or die "cannot open file4 $!\n +"; my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ , sep_char => "\t", always_quote =>1}); print $FILE4 "".<$FILE1>; <$FILE2>; <$FILE3>; my %file2; while (my $row = $csv->getline($FILE2)) { my @fields = @$row; my $id = $fields[0]; $file2{$id}=["",@fields]; } my %file3; while (my $row = $csv->getline($FILE3)) { my @fields = @$row; my $id = $fields[0]; $file3{$id}=["",@fields]; } while (my $row=$csv->getline($FILE1)) { my @fields=@$row; @fields=@fields[map{$_}(0..4)]; my $id=$fields[0]; if (exists $file2{$id}) { if(exists $file3{$id}) { my $fields_ref=\@fields; $csv->print ($FILE4,$fields_ref); } else { } } else { } }

Replies are listed 'Best First'.
Re: Deleting a columnheader
by choroba (Chancellor) on Oct 07, 2011 at 16:37 UTC
    What is the purpose of this line in the code?
    print $FILE4 "".<$FILE1>;

      It will make sure that I will have the columnheader of $FILE1. If I remove it, there will be no headers. I tried to replace that with this code:

      my @columnheadings=split(/\t/,<$FILE1>); @columnheadings=@columnheadings[map{$_}(0..4)]; print $FILE4 "@columnheadings\n";

      Then all the columnheaders were seen in the first cell when it was opened in spreadsheet.

        You were almost there. Just join the array back. And note that map{$_}(0..4) is just the same as 0..4 itself, so you can write:
        print {$FILE4} join("\t", (split /\t/, <$FILE1>)[0 .. 4]),"\n";
Re: Deleting a columnheader
by i5513 (Pilgrim) on Oct 07, 2011 at 17:22 UTC

    Take a look at perlrun and this example, always that your csv files are simple:

    perl -F"\t" -nae 'next if ($_ == EOF and $. != 1); pop @F; print join +"\t",@F,"\n";' file1 file2 file3 > file4

    But, I see now, your bin flag, maybe this is not usable .. I didn't know of csv binary files !

    Update 3: Next time I will read the full question, I missed too about sharing first column value from three files (good exercise to hack that perl one line script)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930211]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-04-24 05:44 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (433 votes). Check out past polls.