good chemistry is complicated,and a little bit messy -LW PerlMonks

### comment on

 Need Help??

In All roads lead to Rome, I pointed out that different languages lead to different ways to looking at problems. One thing I didn't mention was that different types of languages can lead to different solutions. Ben Tilly's phenomenal post Why I like functional programming was a classic example of how a different type of programming background can lead to a radically different solution. In that node, he attached actions to different sections of text. That is certainly not the way I looked at the problem. Interestingly, I would no longer consider such a horrible solution because continued exposure to different ways of looking at problems leads me to learn better ways of dealing with them. Thus, this post...

Getting back to Rome, the example that I put forward was using nested loops to find all elements in one array that existed in another array. The following code snippets all assume that the two arrays being compared have unique elements.

```#!/usr/bin/perl -w
use strict;

my @alphas = qw/ 9 4 3 2 22  13 7  140 95 278/;
my @betas  = qw/ 8 3 4 1 278 94 15 7   19 200/;

my @results;

foreach my \$alpha ( @alphas ) {
foreach my \$beta ( @betas ) {
if ( \$alpha == \$beta ) {
push @results, \$beta;
}
}
}
@results = sort { \$a <=> \$b } @results;
print "Linear search: @results\n";

For small arrays, nested loops are fine, but they don't scale well. Two 10 element arrays require 100 iterations. Two 100 element arrays leads to 10,000 iterations. So, how do you solve the scalability issue? More directly, I am wondering how different types of languages would solve the problem of identifying elements in one array that exist in another. A natural solution in Perl might be to use a hash.

```my @alphas = qw/ 9 4 3 2 22  13 7  140 95 278/;
my @betas  = qw/ 8 3 4 1 278 94 15 7   19 200/;

my @results;

my %alpha;
@alpha{ @alphas } = undef;
foreach my \$beta ( @betas ) {
if ( exists \$alpha{ \$beta } ) {
push @results, \$beta;
}
}

@results = sort { \$a <=> \$b } @results;
print "Hash lookup:   @results\n";

A C programmer, however, might sort one list and perform a binary search. Here's how such a thing might appear if translated to Perl.

```my @alphas = qw/ 9 4 3 2 22  13 7  140 95 278/;
my @betas  = qw/ 8 3 4 1 278 94 15 7   19 200/;

my @results;
my @sorted = sort { \$a <=> \$b } @alphas;

foreach my \$beta ( @betas ) {
my \$index = binary_search( \@sorted, \$beta );
if ( defined \$index ) {
push @results, \$beta;
}
}

@results = sort { \$a <=> \$b } @results;
print "Binary search: @results\n";

sub binary_search {
my ( \$array, \$target ) = @_;
my ( \$low, \$high ) = ( 0, scalar @\$array );

while ( \$low < \$high ) {
use integer;
my \$current = ( \$low + \$high ) / 2;
if ( \$array->[\$current] < \$target ) {
\$low = \$current + 1;
} else {
\$high = \$current;
}
}
if ( \$low < @\$array and \$array->[\$low] == \$target ) {
return \$low;
} else {
return undef;
}
}

Any monks familiar with other languages and styles of programming care to offer other examples? I'd be interested in seeing those in their native language and, if possible, how you would translate those solutions to Perl.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

In reply to TIMTOWTDI and other languages by Ovid

Title:
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.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2022-08-13 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?