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

Good morning Monks, Im fairly new to Perl, I get an output file from a software which is a text file seperate by tabs(although some tabs look longer than others). I can also send an edited version of the file if someone needs the actual file to work with. Here is the code I am using to parse it:

# Opens file $file3 = "c:\\users\\plor\\desktop\\Presc_count2.txt"; $file4 = "c:\\users\\plor\\desktop\\Presc_count3.txt"; open INPUT2, ">", $file4 or die $!; open OUTPUT2, "<", $file3 or die $!; while ($t2 = <OUTPUT2>) { @lines = split /s+/, $t2; print "$lines[0],$lines[1]\n"; } } close(OUTPUT2); close(INPUT2);

When I try to print to test "$lines[0], $lines1" it doesn't give me the output I was expecting, I would've thought it would give me the first two words on that line, instead it prints out either a blank space or nothing(i cant tell if its a space or not). I also tried using split with ('\t') also and I get the same result. Basically I just want each word stored in an element of an array so I can manipulate it. Thanks in advance for any knowledge you guys can pass down to me. -Pao

Replies are listed 'Best First'.
Re: Extracting from output file
by Athanasius (Archbishop) on Aug 15, 2014 at 16:35 UTC

    Hello AllPaoTeam, and welcome to the Monastery!

    You open INPUT2 for writing, i.e. output, and OUTPUT2 for reading, i.e. input. That’s a confusing way to name your variables!

    But the obvious problem is with the first argument to split:

    @lines = split /s+/, $t2;

    which says split on one or more lowercase ‘s’ characters. I think you meant:

    @lines = split /\s+/, $t2;

    which splits on whitespace. Beyond that, I doubt that we can help much without seeing the input text.

    BTW, please consider adopting these best practices:

    • use strict; and declare variables with my
    • use warnings;
    • prefer lexical filehandles: open(my $input2, '>', $file4) or die "Cannot open file '$file4' for writing: $!";

    Hope that helps,

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

      Thanks for the quick response. I tried "@lines = split /\s+/, $t2;" and I still get the same output. I can send an email of the file to anyone who is willing to help me. Im lost on what to do.
Re: Extracting from output file
by AppleFritter (Vicar) on Aug 15, 2014 at 18:01 UTC

    Howdy AllPaoTeam, welcome to the Monastery! Athanasius already pointed out that /s+/ in your code should really be /\s+/ if you intend to split on whitespace. (That's all whitespace, BTW, not just tab characters!) And as he furthermore said, a bit of sample data that shows the problem would be good: it doesn't have to be your entire file, just a portion of it that should be handled correctly but isn't.

    You can also use Text::CSV to parse such files by setting the separator to a tab character, like this:

    #!/usr/bin/perl use strict; use warnings; use feature qw/say/; use Text::CSV; my $csv = Text::CSV->new({ sep_char => "\t", binary => 1, # always a good idea }); while(my $row = $csv->getline(*DATA)) { say "$row->[0],$row->[1]"; } __DATA__ word1 word2 word3 word4 word5 word6

    This is going to be only useful if your words are separated by precisely one tab character, though, since otherwise you'll get empty fields in between words.

      I resolved it.... LOL, just being dumb, I was referencing the a non-existing variable, of course its not going to return anything. Yea I dont use strict and warnings because I dont know how to resolve what its complaining about. Thanks again for your help. -Pao

        Yea I dont use strict and warnings because I dont know how to resolve what its complaining about.

        Ah, that's even more of a reason to use them! If you get errors/warnings that you're not sure what they mean:

        • use diagnostics; to get explanations.
        • Check perldiag (these are the same ones that diagnostics prints, I think).
        • Use the Monastery's Super Search, and/or your favorite web search engine.
        • If you're still not sure, ask a new question on PM!

        You'll learn a lot this way, your Perl will improve, and your scripts will have fewer bugs/unpleasant surprises.

        There's also a book, TheDamian's Perl Best Practices, that may be of interest. It's a bit older by now, but it's got a lot of good advice.