in reply to Re: tab delimited extraction, formatting the output
in thread tab delimited extraction, formatting the output

Thank you kenneth for your great comment and direction. Apprentaly I didn't have CSV package so I learned how to download and install packages too. I am still looking into your script to understand the logic. In the mean time, I added this line at the end but it messed up the output that I was quite happy with that.
elsif ($data[2] eq "mc") { print join("\t",@data[7,8,9,10,11])

how can I make the output of join to appear exctly in front of its related phrase (p). I also looked at sprintf link you sent, couldn't find anything relevant to what I want to do. Thanks again

Replies are listed 'Best First'.
Re^3: tab delimited extraction, formatting the output
by kennethk (Abbot) on Feb 09, 2009 at 22:04 UTC

    Note that in order to get the formatting, you need to cache the previous string in order to determine the indentation.

    #!/usr/bin/perl use strict; use warnings; use Text::CSV; #my $file = "fielded.txt"; my $csv = Text::CSV->new({sep_char => "\t"}); # create a new objec +t open my $fh, "<", $file or die "Unable to open $file: $!"; my($u_value, $p_value, $mc_value) = (undef) x 3; while (my $data_ref = $csv->getline($fh)) { my @data = @{$data_ref}; if ($data[0] eq "'EOU'.") { ($u_value, $p_value, $mc_value) = (undef) x 3; print "\n"; } elsif ($data[2] eq "u") { $u_value = $data[3]; print "\n$u_value"; undef $p_value; } elsif ($data[2] eq "p") { if ($p_value) { print "\n" . ' ' x length $u_value; } $p_value = $data[3]; print "\t$p_value"; undef $mc_value; } elsif ($data[2] eq "mc") { if ($mc_value) { print "\n" . ' ' x length $p_value; } $mc_value = join("\t",@data[7 .. 11]); print "\t$mc_value"; } else { #die "Unexpected line format encountered, $file, @data"; } } close $fh;

      Thank you very much Kenneth. I was looking for that word in perl language (cash) too. I read that "undef" tells perl to remove all formatting (like \n) indicators from the text. So, what does this line do?

      my($u_value, $p_value, $mc_value) = (undef) x 3

      thanks again for your time and great great help
        (undef) x 3 creates a three element array consisting of three null values. Those three values are then mapped to the variables on the left hand side. Strictly speaking, the code my($u_value, $p_value, $mc_value); will have the same effect, but I like to explicitly initialize variables, coming from my programming background.