Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Creating a whitelist

by haukex (Archbishop)
on Jun 15, 2016 at 21:19 UTC ( [id://1165795]=note: print w/replies, xml ) Need Help??


in reply to Creating a whitelist

Hi htmanning,

This triggers the error on every sign up.

I can't reproduce that. If I set $good_orgs="abc\ndef"; and $organization="abc"; then the error is not triggered (Update: same goes for $good_orgs="First\nNational\nBank"; $organization="National Bank"; from your node here - do your variables contain what you think they contain? For example, is $good_orgs a list separated by newlines, like your code indicates by using split /\n/, or is it separated by commas?). There is probably something else going on, so have a look at the Basic debugging checklist to try and figure out what it might be. It also looks like you're coding without strict and warnings, and while those don't directly influence your current code, it's a very good habit to get into, see Use strict and warnings.

It would be very helpful if you could provide some more example information, for example what $good_orgs might look like, and some sample values for $organization, and whether your function should accept them or not. The reason I ask is that your requirements are unclear: If the list of allowed words contains "legal", then your current implementation will also accept an organization named "Illegal Exports".

Note that if $good_orgs contains strings that should be matched literally, and not regular expressions, you should be using /\Q$good_orgs\E/ as explained in, for example, Mind the meta!.

I'm going to wager a guess that $organization may only contain "allowed" words. In that case, here's one way of several that you could do something like that. Note that I'm also making some more assumptions, like that $organization is a comma and/or whitespace separated list, like you seem to be hinting.

use warnings; use strict; my $good_orgs = "First\nNational\nBank\nCompany\nLLC\nlegal"; my %good_orgs = map {lc()=>1} split /\n/, $good_orgs; sub is_good_org { my $organization = shift; my @parts = grep {length} split /[\s;,]+/, $organization; for my $org (@parts) { return unless exists $good_orgs{lc $org}; } return 1; } print is_good_org("LLC") ?"yes":"no", "\n"; # "yes" print is_good_org("Company, LLC") ?"yes":"no", "\n"; # "yes" print is_good_org("National Bank") ?"yes":"no", "\n"; # "yes" print is_good_org("legal company") ?"yes":"no", "\n"; # "yes" print is_good_org("National Bunk") ?"yes":"no", "\n"; # "no" print is_good_org("Illegal Exports")?"yes":"no", "\n"; # "no" print is_good_org("Test LLC") ?"yes":"no", "\n"; # "no"

Note that despite my use of lc, there are some strange cases in Unicode where that isn't actually a case-insensitive comparison. Newer versions of Perl offer the fc function if that's a concern to you.

Updated my code with your example input from here. Update 2: Added the grep {length} in case split leaves some empty elements behind, as would be the case in is_good_org(" LLC").

Hope this helps,
-- Hauke D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1165795]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found