Use the x operator:

#!/usr/bin/perl -w

use strict; #make me really behave

my @words;
my %word_count;

open ( FILE, shift) or die "Cannot open image file $_\n";
while (<FILE>) {
    chomp;                   #get rid of newline (\n)
    @words = split;          #split on spaces
    foreach (@words) {
        $word_count{$_}++;  #add one to word_count for 
                            #every occurance of word
    }
}

foreach (keys %word_count) {
    #print word followed by (word count divided by 5) /s
    #NB: this will ony print /s if word_count is >= 5
    printf("%-05s%s\n", $_, '/' x ($word_count{$_}/5));
}




On 05 Jun 2001 22:52:42 +1000, chris robinson wrote:
> I have the following code.  This code prints a / for each occurance of a
> certain number/word in a file.  How would I change this to print a / for
> each 5 instances of a given word/number?
> 
> Thanks
> 
> #!/usr/bin/perl 
> 
> my $file;
> $file = $ARGV[0];
> open( "file",  "< $file") or die "Cannot open image file $file\n";
> $text = join('', <file>);
> %count = ();
> for (split(/\s/, $text)) {
>  $count{$_} .="/";
>  
> }
> 
> foreach $word (sort keys %count) {
>  next unless $word =~ /\d/;
>  next if $word =~ /p2/i;
>  next if $word =~ /^\#/
> ;  
> 
>  printf("%-05s%s\n", $word, $count{$word});
> 
> }
> 
> 

-- 
Today is Sweetmorn, the 10th day of Confusion in the YOLD 3167
You are what you see.


Reply via email to