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

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;

Replies are listed 'Best First'.
Re^4: tab delimited extraction, formatting the output
by Anonymous Monk on Feb 09, 2009 at 23:20 UTC
    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.
        quick question: why script goes out of the loop and exit if it finds a null value for $p_value? How can I fix it?