gilthoniel has asked for the wisdom of the Perl Monks concerning the following question:

I'm really new to programming and I have two number arrays (ex. array1 = 13, 29, 545 array2 = 5,7,9 etc). I want to see which numbers in array 1 are not divisible by any of the numbers in array 2. Is there an easy way to do this? Thanks Update: Marinersk: *very* new. I've read the basic perl usage info. I know what arrays and hashes are and how to declare variables and print things and the very basic stuff, but I find a lot of the perl manuals go straight from very basic "this is how to declare a variable" to really complex code I don't understand and are missing a lot of the middle ground. Thanks so much! Here is my script, and it works. Thanks for the help! #!/usr/bin/perl use strict; use warnings; my @array1 = (3, 5, 7, 9); my @array2 = (13, 29, 35, 55, 71, 545); foreach(@array1) { my $num1 = $_; foreach(@array2) { my $num2 = $_; if($num2 % $num1 == 0) { print "$num2 is divisible by $num1 \n"; } else { print "$num2 is not divisible by $num1 \n"; } } }

Replies are listed 'Best First'.
Re: Comparing two arrays
by hippo (Archbishop) on Feb 20, 2015 at 23:13 UTC

    There surely is.

    1. Loop over each value in array1
    2. For each of these loop over each value in array2
    3. Inside the two nested loops you can compare every possible pair of values. See if the value from array1 is divisible by the value from array2
    4. ...
    5. Profit!
Re: Comparing two arrays
by marinersk (Priest) on Feb 21, 2015 at 00:26 UTC

    I'm really new to programming

    LOL! Ah, memories. When it was new and exciting.

    So -- How new?

    Let's walk through hippo's stuff and add a few bread crumbs for flavor.

    1. Loop over each value in array1
      Do you know how to do this? Hint: foreach
       
    2. For each of these loop over each value in array2
      Okay, if you can answer the previous one, this one's a gimme, right?
       
    3. Inside the two nested loops you can compare every possible pair of values. See if the value from array1 is divisible by the value from array2
      Ah, now you're going to have to actually make Perl do math for you.
      On second thought, I'll keep this breadcrumb to myself for the moment. Let's see how your code shapes up with the first two points.
      After all, posting your code is a great way to get help. Even if it doesn't work.
      Check that. Especially if it doesn't work. Because that means at least you tried, and we're very likely to see how your thought processes are approaching the issue. Lots to work with that way.
Re: Comparing two arrays
by vinoth.ree (Monsignor) on Feb 21, 2015 at 02:12 UTC

    Hi gilthoniel,

    Welcome to the Monastery.

    As already hippo and marinersk given you the algorithm to achieve your result, they missed to tell you How to check a number is divisible by another number.

    To check if a number is divisible by another number use the modulus operator, %:

    if ($num % $divisor) { # does not divide cleanly } else { # does. }

    I hope you have read the perlintro, So here i am giving you the code skeleton

    #array declaration my @array1 = (....); my @array2 = (....); my ($num,$divisor); foreach $num (array contains dividend) # this loop is for iterator on +e by one { foreach $divisor (array contains divisor) # this loop checks f +irst array elements array divisible by the second array element { if ($num % $divisor) { # does not divide cleanly print "$num is not divisible by $divisor\n"; } else { # does. print "$num is divisible by $divisor\n"; } } }

    All is well. I learn by answering your questions...
      my $var; for $var (...) { }

      is really bad because the two instances of $var are not the same animal. The recommended code is:

      for my $var (...) { }

      The problem is that the first version of the code makes it look like the $var declared before the for loop is the one used in the loop and might even retain the last value it was assigned in the loop. It's not and it doesn't.

      The loop variable ($var in the sample code) is an alias for each value looped over. In some sense it's not a real variable at all! To see the alias in action try:

      use strict; use warnings; my @array = (1 .. 10); for my $var (@array) { $var *= 10; } print "@array";

      Did you notice that the array values were altered! $var becomes each array element in turn. What you do to $var you do to the current array element.

      For the vast majority of Perl code you aren't aware of this aliasing trick. Sometimes it is really useful and sometimes, if you don't know about it, it is really nasty. The same thing happens in map and grep where the default variable $_ is aliased in turn to each list element processed.

      Aside from all that, don't declare multiple variables on a single line. As a general thing it's a clue that you are declaring things in the wrong place, and if it's the right place the declaration is important enough that it should be one variable per line. Along with choosing good variable names, managing variable scope (and therefore where variables are declared) are important parts of good programming style.

      Perl is the programming world's equivalent of English

        Hello GrandFather,

        This is an excellent point! In:

        my $var; for $var (...) {...}

        it’s as though there were an implicit for local $var (...) {...} — except (a) that syntax is illegal, and (b) local doesn’t create an alias (which in C++ would be called a reference) as the for loop does. But thinking of what’s going on as an implicit local does, perhaps, explain why the strict pragma is fooled into thinking $var has been declared:

        14:27 >perl -Mstrict -wE "my $var; for $var (1 .. 3) { say $var; } say + $var;" 1 2 3 Use of uninitialized value $var in say at -e line 1. 14:28 >

        Is there any way to get strict, warnings, or something similar to flag this as a probable error?

        BTW, congratulations on your promotion to Sage!

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Comparing two arrays
by AnomalousMonk (Archbishop) on Feb 21, 2015 at 06:31 UTC

    This is probably not appropriate code for a novice and may, in addition, be problematic because it is not the most maintainable, but...

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use List::MoreUtils qw(all); ;; my @ra_1 = (13, 29, 545); my @ra_2 = (5, 7, 9); ;; my @ra_3 = grep { my $e_1 = $_; all { $e_1 % $_ } @ra_2 } @ra_1; dd \@ra_3; " [13, 29]
    See grep, List::MoreUtils::all and Data::Dump::dd.

    Update: Changed List::MoreUtils::any to List::MoreUtils::all, Data::Dump to Data::Dump::dd in reference links.


    Give a man a fish:  <%-(-(-(-<

Re: Comparing two arrays
by Anonymous Monk on Feb 21, 2015 at 00:47 UTC

    I'm really new to programming and I have two number arrays

    Really? What you posted is not perl code, is not an array ...

    Did you read perlintro?