Hi there, I am new to this forum, and to Perl as a whole. I've been practicing programming in Perl today, using exercises from practicepython.org (yes, I know Python is not Perl, but I like the broad nature of the exercises on there and I had no idea where to look for up to date Perl-specific exercises :-P).

I have just finished doing this exercise: http://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html. The exercise is basically finding the values that two randomly generated lists have in common.

I figured out how to do this using regexps (which are awesome btw) but I haven't been able to reduce the code necessary for matching the values to one line, which is one of the optional objectives of the exercise. Would it be possible to reduce the amount of code necessary to find the common values between the two arrays to only one line of Perl? Or is this as short as it gets? I would also like to know if using regexps for this is less or more efficient (performance-wise) than just using a for loop. This is the code I have as of now:

#!/usr/bin/env perl # This script should check what elements are common between two lists use strict; use warnings; # Lists of random numbers my @input=map {int(rand($_))} (1..100); my @valuesToMatch=map {int(rand($_))} (1..100); print "-----list 1 is:-----\n@input\n-----and list 2 is:-----\n@values +ToMatch\n"; # Match the common values my $matchString = sprintf "\\b%s", join("\\b\|\\b",@valuesToMatch); my @result = join(" ", @input) =~ /$matchString/g; # Return result print "---------------The matching numbers are:---------------\n@resul +t\n" ;

In reply to one line to check for common list values by dollar_sign

Title:
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, details, 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, summary, 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.