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

Noble Perl Monks,

I am a newbie to PERL but have a script that parses a tab delimited text file, and creates multiple text files from it, based upon the first 5 characters found in each line.

What I am trying to figure out is whether there is a way to ignore the first 5 characters in each line when I write each line from the source file into the appropriate log file.

The code that writes the data in the corresponding log is:
open(LOG, "$file"); while (<LOG>) { chomp($_); (@data) = split(/\t/, $_); (@zip) = split(//, $data[0]); $string = ""; for ($i=0; $i<=4; $i++) { # $string .= "$zip[$i]"; $string .= "substr($data[0], 7, 500)"; } # # match 1st 5 with the zip file # if ($z eq $string) { print NEW "$_\n"; } } close(LOG);
This code works but includes the first 5 characters which were use to determine the appropriate LOG to write the line to.

Thanks in advance for your noble assistance!

Replies are listed 'Best First'.
Re: text file parsing with chomp
by toolic (Bishop) on Aug 12, 2009 at 20:21 UTC
    If you want to get the 1st 5 characters of a string, use substr:
    my $string = substr($data[0], 0, 5);

    If this does not answer your question, please:

    • Enclose your code in 'code' tags (see Writeup Formatting Tips).
    • Show a small sample of input data.
    • Show a small sample of output data.
Re: text file parsing with chomp
by ig (Vicar) on Aug 13, 2009 at 01:22 UTC

    It appears you are printing your output with

    print NEW "$_\n";

    If you want to print all but the first five characters of $_ you could change this to

    print NEW substr($_,5),"\n";
Re: text file parsing with chomp
by jethro (Monsignor) on Aug 12, 2009 at 21:02 UTC

    Removing the first 5 chars from a $string:

    substr($string,0,5)='';

      Or alternatively:

      substr $string, 0, 5, '';