Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I determine if an element exists in an array (perl)
    primarykey
    data
    text
    <p>I'm looping through an array, and I want to test if an element is found in another array.</p> <p>In pseudo-code, what I'm trying to do is this:</p> <pre><code>foreach $term (@array1) { if ($term is found in @array2) { #do something here } } </code></pre> <p>I've got the "foreach" and the "do something here" parts down-pat ... but everything I've tried for the "if term is found in array" test does NOT work ...</p> <p>I've tried grep: </p> <pre><code>if grep {/$term/} @array2 { #do something } # this test always succeeds for values of $term that ARE NOT in @array2 if (grep(/$term/, @array2)) { #do something } # this test likewise succeeds for values NOT IN the array </code></pre> <p>I've tried a couple different flavors of "converting the array to a hash" which many previous posts have indicated are so simple and easy ... and none of them have worked.</p> <p>I am a long-time low-level user of perl, I understand just the basics of perl, do not understand all the fancy obfuscated code that comprises 99% of the solutions I read on the interwebs ... I would really, truly, honestly appreciate any answers that are explicit in the code and provide a step-by-step explanation of what the code is doing ... </p> <p>... I seriously don't grok $_ and any other kind or type of hidden, understood, or implied value, variable, or function. I would really appreciate it if any examples or samples have all variables and functions named with clear terms ($term as opposed to $_) ... and describe with comments what the code is doing so I, in all my mentally deficient glory, may hope to possibly understand it some day. Please. :-)</p> <p>...</p> <p>I have an existing script which uses 'grep' somewhat succesfully:</p> <pre><code>$rc=grep(/$term/, @array); if ($rc eq 0) { #something happens here } </code></pre> <p>but I applied that EXACT same code to my new script and it simply does NOT succeed properly ... i.e., it "succeeds" (rc = zero) when it tests a value of $term that I know is NOT present in the array being tested. I just don't get it.</p> <p>The ONLY difference in my 'grep' approach between 'old' script and 'new' script is how I built the array ... in old script, I built array by reading in from a file:</p> <pre><code> @array=`cat file`; </code></pre> <p>whereas in new script I put the array inside the script itself (coz it's small) ... like this:</p> <pre><code> @array=("element1","element2","element3","element4"); </code></pre> <p>How can that result in different output of the grep function? They're both bog-standard arrays! I don't get it!!!! :-(</p> ######################################################################## <h3>addendum ... some clarifications or examples of my actual code:</h3> ######################################################################## <p>The term I'm trying to match/find/grep is a word element, for example "word123". </p> <p>This exercise was just intended to be a quick-n-dirty script to find some important info from a file full of junk, so I skip all the niceties (use strict, warnings, modules, subroutines) by choice ... this doesn't have to be elegant, just simple. </p> <p>The term I'm searching for is stored in a variable which is instantiated via split:</p> <pre><code>foreach $line(@array1) { chomp($line); # habit # every line has multiple elements that I want to capture ($term1,$term2,$term3,$term4)=split(/\t/,$line); # if a particular one of those terms is found in my other array 'array2' if (grep(/$term2/, @array2) { # then I'm storing a different element from the line into a 3rd array which eventually will be outputted push(@known, $term1) unless $seen{$term1}++; } } </code></pre> <p>see that grep up there? It ain't workin right ... it is succeeding for all values of $term2 even if it is definitely NOT in array2 ... array1 is a file of a couple thousand lines. The element I'm calling $term2 here is a discrete term that may be in multiple lines, but is never repeated (or part of a larger string) within any given line. Array2 is about a couple dozen elements that I need to "filter in" for my output. </p> <p>... </p> <p>I just tried one of the below suggestions: </p> <pre><code>if (grep $_ eq $term2, @array2) </code></pre> <p>And this grep failed for all values of $term2 ... I'm getting an all or nothing response from grep ... so I guess I need to stop using grep. Try one of those hash solutions ... but I really could use more explanation and clarification on those.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload