http://www.perlmonks.org?node_id=995888

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

Create two arrays with 55 elements. Both arrays will have the indexes 0 to 54. One will contain the frequency of the indexes; the other will contain the index number (index and value will be the same). You must then manually sort the frequency array in descending order and when swapping values also swap the values in BOTH arrays. Then display the six most frequently occurring numbers and how many times they occurred.

#!/usr/bin/perl use warnings; @freq = (1..10000); for (1..10000) { $freq [1+int(rand(54))]++; } @num = 0..54; @freq = sort{$b <=> $a}@freq;
I am somewhat stuck as to what my next step should be I don't know how to swap elements between arrays. Thanks =)

Replies are listed 'Best First'.
Re: Lottery using arrays
by Athanasius (Archbishop) on Sep 27, 2012 at 04:19 UTC

    Hello perlguru22,

    Since this looks like a homework assignment, I won’t solve it for you, but I will give you a few pointers:

    1. use strict; and declare variables as lexicals: my @freq;, my @num = 0 .. 54;

    2. @freq is specified to contain 55 elements, with indexes 0 to 54. So there is no point in setting aside 10,000 elements up front! In fact, you needn’t assign any elements up front; just declare the array as my @freq; and the elements will be autovivified (created as needed) as they are referenced in the for loop.

    3. For random integers in the range 0 to 54 inclusive, the formula is int(rand(55)) — see rand.

    So, your solution should begin as follows:

    #!/usr/bin/perl use strict; use warnings; my @num = (0 .. 54); my @freq; ++$freq[int(rand(55))] for 1 .. 10_000;

    At this point, you have to sort the frequencies in descending order. The problem specification says:

    You must then manually sort the frequency array in descending order and when swapping values...

    The word “manually”, together with the reference to “swapping values”, rules out any recourse to Perl’s sort function. You will no doubt have been given one or more sorting algorithms in your coursework (for example, bubble sort, selection sort, quicksort — see Sorting_algorithm), and your task is to implement one of these to sort both @freq and @num in parallel.

    Hint: The swapping part of your code should be something like this:

    { my $temp = $freq[$i]; # swap frequencies $freq[$i] = $freq[$j]; $freq[$j] = $temp; $temp = $num[$i]; # swap numbers $num[$i] = $num[$j]; $num[$j] = $temp; }

    Hope that helps,

    Athanasius <°(((><contra mundum

      No temp value is needed when swapping in Perl:
      ($freq[$i], $freq[$j]) = ($freq[$j], $freq[$i]);
      or even (using slices):
      @freq[$i, $j] = @freq[$j, $i];
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Lottery using arrays
by Anonymous Monk on Sep 27, 2012 at 00:56 UTC

    See sort it has lots of examples , also Tutorials: List Processing, Filtering, and Sorting and perldoc -q sort

    #!/usr/bin/perl -- use Data::Dump; @num = @freq = 0..9; dd\@freq,\@num; { my @oldnum=@num; @num = sort { $freq[$b]<=> $freq[$a] } @num; @freq[@oldnum] = @freq[@num]; dd\@freq,\@num; } __END__ ([0 .. 9], [0 .. 9]) ( [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0], )
Re: Lottery using arrays
by Anonymous Monk on Sep 27, 2012 at 14:23 UTC
    Since it obviously is homework ... work with your instructor and perhaps your classmates to come up with your solution, even if it is possible to find your answer somewhere on the Internet.