in reply to Re^2: check whether a array element is equal to a string.
in thread check whether a array element is equal to a string.

You mean, it's faster to do N * 2 comparisons than 2 * N comparisons?

If you really care, benchmark. Both will do the same amount of comparisons, but the grep probably uses less opcodes. On top of that, the grep only does one assignment/push per array. Regardless of the number of matches.

Not that I care. I find the grep version clearer: the important part is the modification (setting or pushing) of the arrays, and they are in front with the grep solution, instead of buried inside the foreach.

And in general, I'd prefer a solution that doesn't need indentation over one that goes two levels deep. (Deepness of indentation is a rough estimate of complexity (yeah, yeah, hold the counterexamples)).

  • Comment on Re^3: check whether a array element is equal to a string.

Replies are listed 'Best First'.
Re^4: check whether a array element is equal to a string.
by ritchie (Initiate) on Jan 11, 2011 at 14:32 UTC
    My code reads from a text file, populates an array, iterates through the array and calls some functions if the array conatins elements. I have enclosed my code please let me know if there a better way to do this or shorten the length of code.
    $ENV=SIT; my @EG01=(); my @EG02=(); my @EG03=(); my @EG04=(); my @EG05=(); my @EG06=(); my @EG07=(); my @EG08=(); my @EG09=(); my @EG10=(); my @EG11=(); my @EG12=(); my @EG13=(); my @EG14=(); my @EG15=(); my @EG16=(); my @EG17=(); my @EG18=(); my %seen1=(); my %seen2=(); my %seen3=(); my %seen4=(); my %seen5=(); my %seen6=(); my %seen7=(); my %seen8=(); my %seen9=(); my %seen10=(); my %seen11=(); my %seen12=(); my %seen13=(); my %seen14=(); my %seen15=(); my %seen16=(); my %seen17=(); my %seen18=(); if(-e "a.txt") { open (INPUT, "<a.txt") || die "can't open file 11.2.txt\n"; chomp(@objects=<INPUT>); foreach $artefact (@objects) { $artefact=~ s/.bar//; open (INPUTFILE, "</./.\\b\\c\\d\\e\\$artefact.txt") || di +e "cant open file\n"; @lines=<INPUTFILE>; foreach $line (@lines) { $value = $line; if ($value=~ /$ENV.EGS=*/) { $new=$value; $new3=(split /\=/, $new)[-1]; push(@new2, $new3); print "artefact - $artefact to be deployed in eg-$ +new3\n"; chomp(@new2); foreach $newline(@new2) { if($newline eq 'x1') { push(@EG01, $artefact); } if($newline eq 'x2') { push(@EG02, $artefact); } if($newline eq 'x3') { push(@EG03, $artefact); } if($newline eq 'x4') { push(@EG04, $artefact); } if($newline eq 'x5') { push(@EG05, $artefact); } if($newline eq 'x6') { push(@EG06, $artefact); } if($newline eq 'x7') { push(@EG07, $artefact); } if($newline eq 'x8') { push(@EG08, $artefact); } if($newline eq 'x9') { push(@EG09, $artefact); } if($newline eq 'x10') { push(@EG10, $artefact); } if($newline eq 'x11') { push(@EG11, $artefact); } if($newline eq 'x12') { push(@EG12, $artefact); } if($newline eq 'x13') { push(@EG13, $artefact); } if($newline eq 'x14') { push(@EG14, $artefact); } if($newline eq 'x15') { push(@EG15, $artefact); } if($newline eq 'x16') { push(@EG16, $artefact); } if($newline eq 'x17') { push(@EG17, $artefact); } if($newline eq 'x18') { push(@EG18, $artefact); } } close INPUT; } } close INPUTFILE; } } my @EG01 = grep{! $seen01{$_}++} @EG01; $size_EG01=@EG01; if($size_EG01>0){ system "java abcd"; foreach(@EG01){ system "perl ../123/z.pl $_.bar $new3"; } } my @EG02 = grep{! $seen02{$_}++} @EG02; $size_EG02=@EG02; if($size_EG02>0){ system "java abcd"; foreach(@EG02){ system "perl ../123/z.pl $_.bar $new3"; } } my @EG03 = grep{! $seen03{$_}++} @EG03; $size_EG03=@EG03; if($size_EG03>0){ system "java abcd"; foreach(@EG03){ system "perl ../123/z.pl $_.bar $new3"; } } my @EG04 = grep{! $seen04{$_}++} @EG04; $size_EG04=@EG04; if($size_EG04>0){ system "java abcd"; foreach(@EG04){ system "perl ../123/z.pl $_.bar $new3"; } } ... .... ....
Re^4: check whether a array element is equal to a string.
by raybies (Chaplain) on Jan 10, 2011 at 16:28 UTC

    You mean, it's faster to do N * 2 comparisons than 2 * N comparisons?

    true. same number of comparisons. I suppose I was thinking in terms of fitting the comparison elements into the processor's register space. sorry, old asm programmer here. :) I agree it's a pretty cool solution. Thanks.