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

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions [solved]Perl: Printing line numbers to matched strings and hashes.
# 1  
Old 10-24-2012
[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).

Code:
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.
# 2  
Old 10-25-2012
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:

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

and

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

I hope this helps.

bakunin
# 3  
Old 10-25-2012
Quote:
Originally Posted by bakunin
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:

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

and

Code:
$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:
Code:
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:
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! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Replace a multi-line strings or numbers

Hi I have no experience in Unix so any help would be appreciated I have the flowing text 235543 123 45654 199 225 578 45654 199 225 I need to find this sequence from A file 45654 199 225 (22 Replies)
Discussion started by: khaled79
22 Replies

3. Homework & Coursework Questions

Lex: analyzing a C file and printing out identifiers and line numbers they're found on

Florida State University, Tallahassee, FL USA, Dr. Whalley, COP4342 1. The problem statement, all variables and given/known data: Create a lex specification file that reads a C source program that ignores keywords and collects all identifiers (regular variable names) and also displays the line... (3 Replies)
Discussion started by: D2K
3 Replies

4. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

5. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

6. Shell Programming and Scripting

Need help in Perl Script for printing next line

I got multiple of documents in which i have to extract a line coming after a ID..The scenario is Customer ID: none VT : 002/89 Customer ID: Yes VT: 001/89 Customer ID: none VT: 006/85 Customer ID: Yes VT: 003/56 I have to extract the id which is coming after YES..The output... (7 Replies)
Discussion started by: rajkrishna89
7 Replies

7. Shell Programming and Scripting

PERL - printing a hash of hashes to screen

Hi there I have a hash of hashes made up of the following data bge0|100|half|10.36.100.21 bge1|1000|full|10.36.100.22 bge2|1000|full|10.36.100.23 which when i turn into a hash, would look like this inside the system bge0 -> nic_speed -> 100 nic_duplex -> half ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. Shell Programming and Scripting

Hashes help in Perl

Hi, I am stuck at this problem where part of my code would store all the websites that has been accessed by a user. I pull these values from a log file. I want to create a HASH of HASHES ? (Please correct me if this is not the right approach) where I would store all the hits to website with... (4 Replies)
Discussion started by: Dabheeruz
4 Replies

9. Shell Programming and Scripting

printing an empty line in a file (perl)

I know this must be really easy, but i can't get it to work I've got a perl script, with a file. I want to print an empty line, and the following doesn't seem to work: print nameoffile "\n" thanks for your help!! (3 Replies)
Discussion started by: kfad
3 Replies

10. UNIX for Dummies Questions & Answers

Printing line numbers

Maybe this question is out there, but I searched and didnt see it. To print my files I use more filename | lpr -Pprinter I would like to print my scripts with line numbers. How do I do this? (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question