Can any one please help I am trying to sort each item in every line and count the common (non case sensitive) and at the end printing all the unique alphabetically.
Here is what i did ... I can print all the lines but struck to sort each line some were:
I am getting the output as:
I want to count all the items, expected result:
------------
Could you please post desire output ?
Chirel
April 29, 2011, 3:35am
4
Hi,
like everytime with perl there are lot's of way to solve this.
i give you one, not optimized, using hash tables.
I should use at least map but i'm in a lazy day
#!/bin/perl
@strings = ("Shirt pants candy pants keychain",
"shirt shirt Candy gum sticker gum",
"flowers shirt candy card card");
%hash=();
foreach $line(@strings){
@spl = split(/ /, $line);
foreach $temp (@spl) {
$hash{lc $temp}++;
}
}
foreach $i (keys(%hash)) {
print "$i $hash{$i}\n";
}
Output:
flowers 1
gum 2
candy 3
card 2
pants 2
sticker 1
keychain 1
shirt 4
try this,
#!/usr/bin/perl
@strings = ("Shirt pants candy pants keychain",
"shirt shirt Candy gum sticker gum",
"flowers shirt candy card card");
foreach (@strings)
{
@flds=split;
foreach(@flds) {$hash{lc $_}++;}
}
print $_," " , $hash{$_},"\n" foreach keys(%hash);
Pravin and Chirel - both soln works, thanks so much.