Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help finishing a perl program
New Posts  All Forums:Forum Nav:

Help finishing a perl program

post #1 of 3
Thread Starter 
ok so i have an assignment and its all done except for one little thing i cant figure out...im sure its somthing simple i just cant see it
here is the program i have to do
Quote:
2. Write a program that reads a series of words (one word per line) until end-of-input, then prints a summary of how many times each word was seen. Sort the output in lexicographic order.
If the input were
Fred
Barney
Fred
Dino
Wilma
Fred
The output would be:
Barney: 1, Dino: 1, Fred: 3, Wilma: 1
and here is my code
Code:
my(@words, %count, $word);
chomp(@words = <STDIN>);

foreach $word (@words)
{
        $count{$word} += 1;
}

foreach $word (sort keys %count)
{
print ("$word: $count{$word} " );
}

my output is like this
Code:
Barney: 1 Dino: 1 Fred: 3 Wilma: 1
so it works perfectly except i wanted to make it comma separated without having a comma before the first element or after the last i cant seem to figure it out
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
post #2 of 3
This is quite simple. Instead of printing each iteration out, append it to the end of a string with a comma afterwards. eg
Code:
$string .= "$word: $count{$word}, "

Then outside of the foreach loop, print the entire string, cropping out the trailing comma. Use substr for that.

Also, if you're using strict, then make sure you declare your variable before the foreach.
Edited by Plan9 - 2/26/13 at 3:43am
post #3 of 3
Thread Starter 
nvmi got it thanks
Edited by Hartk1213 - 2/28/13 at 12:33am
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
The Prodigy
(16 items)
 
 
Dream Rig Contest
(11 items)
 
CPUMotherboardGraphicsRAM
[AMD] Athlon II x2 255 [Gigabyte] GA-78LMT-SP2 [Onboard] ATI Radeon 3000 [Crucial] Ballistix 8GB (2x4GB) 
Hard DriveHard DriveHard DriveOptical Drive
[Western Digital] Blue 320GB [Maxtor] 320GB soon to be adding 4TB from WD Red [Lite-On] DVD Player/Burner 
CoolingOS
[Stock] [Mircrosoft] Windows 7/ Home server 2011 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help finishing a perl program