Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: To infinity and beyond: Embedding loops

by antirice (Priest)
on Jul 16, 2003 at 10:14 UTC ( #274755=note: print w/replies, xml ) Need Help??


in reply to To infinity and beyond: Embedding loops

If anyone sends me a RTFM advisory they can stuff my output in there I/O and die.

Well aren't you a ray of sunshine? :P

Where to begin? Well, it shouldn't loop forever. Although, I expect on a large @line and a large @angel it should seem as if it's taking forever. For instance, suppose both are 10_000 elements long. That means you would have 100_000_000 comparisons. When everything is finally finished, @b will be absolutely enormous. For illustrative purposes, let's suppose @line = @angel and that every element in @line is unique. After the loop, @b will contain scalar(@line)**2 - scalar(@line) elements. Needless to say, that's a lot. Also, you're aware that you're pushing indexes into the array, right? You're skipping index 0 on both arrays with your loops. Your comparison checks to see whether or not $k contains the regex contained in $j. This can be a nightmare from a maintainence standpoint since your program will die anytime $j contains a string with an unmatched parenthesis or left bracket. I doubt this is what you really want. You should at least use \Q and \E if you want to see whether or not $k contains the string in $j. If you just wanted to see if they're equal, you should just use lc($k) eq lc($j). Of course, if you want to see whether or not they're actually equal to one another, there's a better way to do it. Here's some code that should be a bit faster and make @b and @a contain what you actually want (except you get to fill in some blanks):

# this part is just so you don't get an "requires explicit # declaration" error my @lines = getLines(); my @angel = getAngel(); my @a;my @b; # ok...here's some magic # we create a hash whose keys come from @lines. Why # do this? Since checking an element in a hash is O(1) # it is much faster than searching the array @lines # ...also, on a side node the map is so we do case # insensitive checking when we look for exists my %hash; @hash{map(lc,@lines)} = (); # ok, now we create a loop that will set $_ to the numbers # from 0 to $#angel. We could also do for (@angels) to get # the actual values, but since you pushed the index of # matches into @a and @b, I opted for this option instead. for (0..$#angel) { # ok, since we didn't actually set each hash element to any # value, we can't compare it against anything...BUT, we can # check to see whether or not it exists if (exists $hash{lc($angel[$_])}) { # hey, we have a match, take care of it } else { # darn...not a match :-/ } }

Since I don't want to stuff your input into my I/O and die, I will not tell you to RTFM. Also, if you have time, don't read perlre, perldata or How can I find the union/difference/intersection of two arrays?.

Updated: Added comments to the code so it is more understandable to a fellow monk who asked for clarification of how exactly it works.

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2022-01-29 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?