perl file, one line code include "length, rindex, substr", slow

Hi Everyone,

[root@]# cat a.txt
a;b;c;64O
a;b;c;d;ee;f

[root@]# cat a.pl
#!/usr/bin/perl
use strict;
use warnings;

my $tmp3 = ",,a,,b,,c,,d,,e,,f,,";
open(my $FA, "a.txt") or die "$!";
while(<$FA>) {
        chomp;
        my @tmp=split(/\;/, $_);
        if ( ($tmp[3] =~ m/^(64O)/i) || ($tmp[4] =~ m/^(64O)/i) || ((!$tmp[3]) && (!$tmp[4])) ) {
                #print $_;
        } else {
                if ( ((length($tmp[3]) == 1) && (length($tmp[4]) > 1) && (rindex($tmp3, ",,".substr($tmp[3],0,length($tmp[3])).",,") != -1)) ) {
                        print "$tmp[0];$tmp[1];$tmp[2];$tmp[3];$tmp[4]";
                }
        }
}
[root@]#

[root@]# ./a.pl
a;b;c;d;ee
[root@]#

this a.txt in real word is having lots of lines, i find the line with "length, rindex, substr" this if code, runs very slow. any better way to improve it?

Thanks

What exactly are you trying to do here ?

tyler_durden

Sorry for the information not giving clearly.

case 1:
for this code:

if ( ($tmp[3] =~ m/^(64O)/i) || ($tmp[4] =~ m/^(64O)/i) || ((!$tmp[3]) && (!$tmp[4])) ) {
}

whether can simplify it? like we can combine it to $tmp[3]|$tmp[4] =~ m/^(64O)/i), and (!$tmp[3]) && (!$tmp[4])) to (!$tmp[3]|$tmp[4]) ?

case 2:

my $test="c";
rindex(",,a,,b,,c,,", ",,".substr($test,0,length($test)).",,") != -1

if any simply way to do that, instead of rindex, substr.

Thanks

It's still not clear enough.

Ok, let's do this - you have this file:

[root@]# cat a.txt
a;b;c;64O
a;b;c;d;ee;f

Just describe, in simple, plain English, what you want to do with this file.

Keep that Perl code aside for a moment.

tyler_durden