Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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; #'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.

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

In reply to Re: To infinity and beyond: Embedding loops by antirice
in thread To infinity and beyond: Embedding loops by a_mere_kat

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

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

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

    Results (74 votes). Check out past polls.