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

Gurus, I pushing two columns binded together into an array. However, I am using substr instead of split. Is there a better way to do this? Below is the code I am using.
open TRACE, "C:/test/trace_11122008.txt" or die "Couldn't open trace_1 +1122008.csv file: $!"; my @trace_row; while (<TRACE>) { print chomp($_); push @trace_rw, {trace_cur => substr($_,0,10), trace_orig => substr($_,11,18)}; }
Any suggestions would be greatly appreciated. Dave

Replies are listed 'Best First'.
Re: I need your opinion on pushing two columns into an array
by ccn (Vicar) on Nov 16, 2008 at 17:08 UTC
    push @trace_rw, [unpack 'A10A8', $_] while <TRACE>;

    or

    my @trace_rw = map { [unpack 'A10A8', $_] } <TRACE>;
      Thanks. What I'm try to do is search a text file for certain numbers and if it finds it in the array to replace it. The problem I'm now having is in my if statement. The $stmt_tr keeps on changing to the array variable when it should keep the number I'm trying to find in the array.
      while (<CIPA>) { my $string = $_; my $stmt_tr = substr($_,126,10); if ((substr($string,126,1) =~ m/5/) && (substr($string,98,8) = +~ m/20081112/)) { foreach my $x (@trace_row) { if ($stmt_tr = $x->{trace_orig}) { print $x->{trace_orig} . "\n"; } } print $string; ++$rcount; print $rcount . "\n"; } }
        if ($stmt_tr = $x->{trace_orig}) { is an assigment

        use
        if ($stmt_tr == $x->{trace_orig}) { 'double =' for numbers
        if ($stmt_tr eq $x->{trace_orig}) { 'eq' for strings

        while (<CIPA>) { my $string = $_; my $stmt_tr = substr($_,126,10); if ((substr($string,126,1) =~ m/5/) && (substr($string,98,8) = +~ m/20081112/)) {

        That should be:

        while ( my $string = <CIPA> ) { my $stmt_tr = substr $string, 126, 10; if ( substr( $string, 126, 1 ) == 5 && substr( $string, 98, 8 +) == 20081112 ) {
        I found the cheesy mistake that is make that happen "=". I should be using "=~".
Re: I need your opinion on pushing two columns into an array
by GrandFather (Saint) on Nov 16, 2008 at 20:04 UTC

    print chomp($_); probably isn't doing what you expect. chomp returns the number of characters it removed so most likely your print simply generates a long string of ones.

    I notice that the file has a .csv extension which implies a CSV file. Generally it is a bad idea to hand parse CSV because there are a number of quite nasty edge cases to deal with. You may resolve many of you problems by using Text::CSV or Text::xSV.


    Perl reduces RSI - it saves typing
Re: I need your opinion on pushing two columns into an array
by Anonymous Monk on Nov 16, 2008 at 17:02 UTC
    Depends on what you're doing ... but you could
    push @trace_rw, [ substr($_,0,10), substr($_,0,18) ]; ... $trace_rw[0][0]; $trace_rw[0][1]; ... use constant TRACE_CUR => 0; use constant TRACE_ORIG => 1; $trace_rw[0][TRACE_CUR]; $trace_rw[0][TRACE_ORIG];
    to save a little on memory/speed