[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools.

This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input.

Sample input from <> operator:
Hello World
This is hello
a sample program
this program.

The output from above should be:
hello: 2 times, lines 1, 2
world: 1 times, lines 1
this: 2 times, lines 2, 4
sample: 1 times, lines 3
program: 2 times, lines 3, 4
is: 1 times, lines: 2

Due to this being a current assignment I cannot submit all of my code for obvious reasons. I have the hash count for the instances correct. I'm having issues with finding and printing the lines (concatenated together, etc).

foreach $tempL (@line)
{
    @cleanWords = split(/\s+/, $tempL);
    for ($i=0; $i <= $#cleanWords; $i++)
    {
        if ( exists($occurCount{$cleanWords[$i]}) )
        {
            $occurCount{$cleanWords[$i]}++;
        }
        else
        {
            $occurCount{$cleanWords[$i]} = 1;
        }
        if ( length($cleanWords[$i]) > $currLongest )
        {
            $currLongest = length($cleanWords[$i]);
        }
        foreach $tempW (@line)
        {
            $newlineCount++;
            if ( index($tempW, $cleanWords[$i])>0 )
            {
                $concLNums = $concLNums . "$newlineCount, ";
            }
        }

    }
    push @lineNums, $concLNums;
    $concLNums = "";
    $concLNums = 0;
}
    my $counterUp = 0;
    my $wordprint;
    foreach $wordprint ( sort( keys(%occurCount) ) ) 
    {

         printf ("%${currLongest}s: %4d times, lines: %0s\n", $wordprint, $occurCount{$wordprint}, $lineNums[$counterUp]);
         $counterUp++;
    }

Any hints greatly appreciated. I know it's not difficult but I'm not focusing enough because I've spent hours changing it.

You read in the lines, one after the other, in "$tempL", right? Now, if you have a counter which increases every time you start work on a new line, it would contain the line number, yes?

You only need to store this number every time you increase the counter in these lines:

$occurCount{$cleanWords[$i]}++;

and

$occurCount{$cleanWords[$i]} = 1;

I hope this helps.

bakunin

Thanks for the reply. I made some progress with creating another hash and using the value from the %occurCount hash as the key to the new %lineConcat hash. However, my output is still interesting. Any ideas?

Here's my input and output from command line:

Test:desktop D2K$ perl working.pl 
Hello World
Hello again.
again:    1 times, lines: 2
hello:    2 times, lines: 3, 4, 
world:    1 times, lines: 2
Test:desktop D2K$ 

Here's the updated code:

my %lineConcat;

$lNum = 1;

foreach $tempL (@line)
{
    @cleanWords = split(/\s+/, $tempL);

        
    
    for ($i=0; $i <= $#cleanWords; $i++)
    {
        if ( exists($occurCount{$cleanWords[$i]}) )
        {
            $occurCount{$cleanWords[$i]}++;
            foreach (@line)
            {
                if (/$_/)
                {
                    $concLNums = $concLNums . "$lNum, ";
                    $lNum++;
                    $lineConcat{$occurCount{$cleanWords[$i]}} = $concLNums;
                }
                else
                {
                    $lNum++;
                }
                
                
            }
            $lNum = 1;
        }
        else
        {
            $occurCount{$cleanWords[$i]} = 1;
            $lineConcat{$occurCount{$cleanWords[$i]}} = "$lNum";
        }
        
        if ( length($cleanWords[$i]) > $currLongest )
        {
            $currLongest = length($cleanWords[$i]);
        }
        
        
    $lNum++;
    }

---------- Post updated at 08:19 PM ---------- Previous update was at 01:14 PM ----------

Solved my problem. Thanks for the help! :slight_smile: