Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) 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.


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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [davido]: yeah, umount -f isn't powerful enough.
    [tye]: probably something in the init subsystem that does the mounting that you could disable and reboot.
    davido needs to close laptop to board flight home from yapc.
    [davido]: I'll look into it in a few hours probably.
    [davido]: when i do get to that point I think I'll do it in a vm snapshot just in case. :)
    [oiskuu]: tye, you were right: loginuid/sessionid are part of task struct if compiled with AUDITSYSCALL. I have some doubts if you should actually depend on that feature.
    [Corion]: oiskuu: Depends on what you want to do with that information
    [tye]: I'm not depending on that feature. But I could in this environment. I'm using getlogin(). shrug
    [Corion]: For benign logging (which user started this DB instance), it's OK

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (7)
    As of 2017-06-23 20:09 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (555 votes). Check out past polls.