in reply to Re: Perl Script performance issue
in thread Perl Script performance issue

Perl's in built grep works on elements. How do i make i behave the way i intend? Do i have to store the line into an array or something? Confused..please help..

Replies are listed 'Best First'.
Re^3: Perl Script performance issue
by Athanasius (Archbishop) on Dec 17, 2015 at 13:54 UTC

    Hello again Tara,

    You have the line:

    $final_data = ` grep "$key_pattern" "${indir}/${lkp_file_name}" |cut - +d"|" -f"$data_location"`;

    Here is one way to write this in pure Perl (untested):

    my $file = $indir . '/' . $lkp_file_name; open(my $fh, '<', $file) or die "Cannot open file '$file' for reading: $!"; my @matched_lines = grep { /\Q$key_pattern/ } <$fh>; close $fh or die "Cannot close file '$file': $!"; my @fields; for my $line (@matched_lines) { my $field = (split /\|/, $line)[$data_location - 1]; chomp $field; push @fields, $field; } $final_data = join "\n", @fields;

    (There should also be some error handling to deal with the possibility that a matched line contains an insufficient number of fields.)

    Hope that helps,

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