Hi I'm new to perl (2 weeks). I'm understanding everything in my textbook just fine, but the projects our teacher gives us are way harder than anything we've covered. He wants us to do the following.

Write a Perl program that will read from two files specified on the command line. Both files will be a list of positive integers (one entry per line) and terminated with a zero. Your program should (ignoring the zeros) output, in the given order, each of the numbers in the second file followed by a single space and then the number of times that value appears in the first file.

If testfile1 has (5 3 2 7 5 4 3 2 5 0) in it, and testfile2 has (5 2 3 1 0) running the program will look like

$ ./frequency testfile1 testfile2 5 3 2 2 3 2 1 0

Here's my attempt at a solution

#!/usr/bin/perl -w #use strict; open(FILE2, "<testfile2"); #giving testfile2 data a filehandle @array2 = <FILE2>; #saving filehandle to array pop @array2; #getting rid of 0 at end %hash2 = @hash2{@array2}; #storing contents of testfile2 without 0 to #a hash open(FILE1, "<testfile1"); #giving testfile1 data a filehandle @array1 = <FILE1>; #saving filehandle to array pop @array1; #getting rid of 0 at end foreach $number (keys %hash2) { #go through each of the hashs' keys #and assign them to $number for duration of loop some keys might be #used more than once if ($number eq search(@array1)) {#check to see if string $number #equals each iteration of subroutine search being passed values of #@array1. Would have used another foreach loop here, but parentheses #and curly brace placement won't allow it. $hash2{number} += 1; } } sub search { #subroutine search gets one element of @array1 at a #time and returns it. Then moves on to the next value in that array. foreach (@_) {} return $_; } #Ideally here would like to print the array followed by the hash #elements print (@array, %hash2);

I get no error messages. I really can't emphasize enough that the 13 pages on hashes in the llama book do not address anything nearly as complex as searching through every key of a hash AND comparing those keys to each element of an array. More specifically several things are not done to the same hash. There is an example that does this:

foreach $person (sort keys %book) { if ($books{person}) { print $person has $books{$person} items\n"; } }

so I have a similar method checking to see if the keys of my hash match each value in an array. I appreciate the solutions provided by others, but what is wrong with my solution?


In reply to frequency strings 2 files by philbertcheese86

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.