Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Answer: How to find out if X is an element in an array?

by russmann (Initiate)
on Dec 08, 2001 at 02:30 UTC ( #130348=categorized answer: print w/replies, xml ) Need Help??

Q&A > arrays > How to find out if X is an element in an array? - Answer contributed by russmann

Here is a super-fast script i just wrote using this information. It's for finding email addresses (elements) in one file that aren't in another.
#!/usr/bin/perl # File definitions $file1 = "first_file.txt"; # File with emails that might be duplicated + on 2nd file $file2 = "second_file.txt"; # These emails have already been used. $file3 = "finalemails.txt"; # Final product - emails that havn't been +used yet. # Load arrays with file contents open(FILE, "<$file1") || die "Can't open $file1\n"; @updatelist = <FILE>; close(FILE); open(FILE, "<$file2") || die "Can't open $file2\n"; @storelist = <FILE>; close(FILE); # Set %elements hash to 1 for each address in store list. foreach (@storelist) { $elements{$_} = 1; }; # Loop to create the final array of emails foreach $update(@updatelist) { if (!find_in_store($update)) { push @finallist, $update; } } # Write final array to file open(FILE, ">$file3") || die "Can't open $file3\n"; print FILE @finallist; close(FILE); # Routine to check for existance of an address. sub find_in_store { $element = $_[0]; if (exists $elements{$element}) { return(1); } else { return (0); } }

Replies are listed 'Best First'.
Re: Answer: How to find out if X is an element in an array?
by atcroft (Abbot) on Dec 08, 2001 at 12:32 UTC

    This may be simplistic, but what about the following code. Anyone?

    # # Given: first value passed is the element to check for # Given: remaining values are the array to check in # sub find_in_list { my ($element, @list2check) = @_; return(scalar(grep($element, @updatelist))); }
      Do this or if you search for "1" it'll find "1" as well as "123"...
      return(scalar(grep(/^$element$/, @updatelist)));
      -julian geek
Re: Answer: How to find out if X is an element in an array?
by sparkyichi (Deacon) on Dec 08, 2001 at 03:25 UTC
    Is it safer to undef FILE before you reuse it or does it make a difference? I know from a readability standpoint, you might consider using different handles:
    open(FILE1, "<$file1") || die "Can't open $file1\n"; open(FILE2, "<$file2") || die "Can't open $file1\n"; open(EMFILE, "<$file3") || die "Can't open $file1\n";
    I guess from the efficiency point of view that it would use less memory if you either use the same file handle or undef them after you close the file. Is that true?

    Sparky
Re: Answer: How to find out if X is an element in an array?
by cLive ;-) (Prior) on Dec 08, 2001 at 05:57 UTC
    Here's a similar approach:
    # snip my %hash; $hash{$_}=1 for (@updatelist); $hash{$_}=0 for (@storelist); for (keys %hash) { push @finallist, $_ if ($hash{$_}); }
    ie: "make list of new addresses, but cross off those already used".

    ..02

    cLive ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-10-19 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (207 votes). Check out past polls.

    Notices?