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

Hello Wise and Kind Monks: I need a simple program to parse a text file, and modify the text file. The text looks like this.

1.7 MB | Z: \PeachTree\Forms
601.3 KB | Z: \PeachTree\Reports
149.0 KB | Z: \PeachTree\Letters
593.4 MB | Z: \Marketing Department
425.4 MB | Z: \Marketing Department\Presentations
92.9 MB | Z: \Marketing Department\Other

I need to split it on the "|". And then count the "\" in the right hand string and prefix the right hand side with that number of "|". Want to load the data into excel and make it look pretty.

This is my code.
open ( txtFileIn, $ARGV[0] ) || die ("Could NOT open " . $ARGV[0] . + ".\n"); @txtFileIn = <txtFileIn>; print scalar @txtFileIn ; foreach $row (@txtFileIn) { chomp($row); print "\n" . index ($row , '|'); ($size, $item) = split /|/, $row; print "\n**",$row, $size, $item ; chomp($size); chomp($item); $cnt = $item =~ s/(\\)/$1/gi; $item .= ('|' x $cnt ) . $item ; # print "\n" , $size . $item, $cnt; } #foreach row close txtFileIn;
The split is not working. I have other code when split works just fine. But I do not see my silly mistake here. This what the code returns.
12 ** 1.7 MB | Z: \PeachTree\Forms 12 ** 601.3 KB | Z: \PeachTree\Reports 12 ** 149.0 KB | Z: \PeachTree\Letters 12 ** 593.4 MB | Z: \Marketing Department 12 ** 425.4 MB | Z: \Marketing Department\Presentations 12 ** 92.9 MB | Z: \Marketing Department\Other


Requesting your kind assistance

kd

Replies are listed 'Best First'.
Re: simple simple split
by moritz (Cardinal) on Nov 01, 2007 at 16:52 UTC
    | is a special char in a regex, if you want to match a | verbatimely, escape it:

    split m/\|/, $row;

    Read perlretut and then perlre for more details.

      many thanks Monks.

      I will now stop banging my head.

      kd
Re: simple simple split
by skx (Parson) on Nov 01, 2007 at 16:52 UTC
    Change this:
    ($size, $item) = split /|/, $row;
    to this:
    ($size, $item) = split /\|/, $row;

    You're splitting on a regular expression and '|' has special meaning there.

    Steve
    --
Re: simple simple split
by BrowserUk (Patriarch) on Nov 01, 2007 at 16:52 UTC
Re: simple simple split
by eff_i_g (Curate) on Nov 01, 2007 at 17:17 UTC
    I like split quotemeta '|', ....
    Also see \Q and \E in perlre.