Perl - Sort and Count foreach line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Sort and Count foreach line
# 1  
Old 04-28-2011
Perl - Sort and Count foreach line

Quote:
@strings = ("Shirt pants candy pants keychain",
"shirt shirt Candy gum sticker gum",
"flowers shirt candy card card");
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:

Quote:
foreach $line(@strings){
@spl = split(/ /, $line);
foreach $temp (sort {$a cmp $b}(@spl)){
print "$temp\n";
}
}
I am getting the output as:
Quote:
Shirt
candy
keychain
pants
pants
Candy
gum
gum
shirt
shirt
sticker
candy
card
card
flowers
shirt
I want to count all the items, expected result:
Quote:
shirt 4
candy 3
card 2
------------

Last edited by sureshcisco; 04-29-2011 at 04:15 AM..
# 2  
Old 04-29-2011
Could you please post desire output ?
# 3  
Old 04-29-2011
Updated
# 4  
Old 04-29-2011
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 Smilie

Code:
#!/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:
Code:
flowers 1
gum 2
candy 3
card 2
pants 2
sticker 1
keychain 1
shirt 4


Last edited by Chirel; 04-29-2011 at 04:37 AM.. Reason: Add output.
This User Gave Thanks to Chirel For This Post:
# 5  
Old 04-29-2011
try this,
Code:
#!/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);

This User Gave Thanks to pravin27 For This Post:
# 6  
Old 04-29-2011
Pravin and Chirel - both soln works, thanks so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print perl hash value in foreach loop

Experts - Any advice on how to get a hash value in a foreach loop? Values print correctly on standalone print statements, but I can't access value in foreach loop. See sample code below and thanks in advance. foreach my $z (sort keys %hash) { for $y (@{$hash{$z}}) { print "$z... (6 Replies)
Discussion started by: timj123
6 Replies

2. UNIX for Advanced & Expert Users

Sort words based on word count on each line

Hi Folks :) I have a .txt file with thousands of words. I'm trying to sort the lines in order based on number of words per line. Example from: word word word word word word word word word word word word word word word word to desired output: word (2 Replies)
Discussion started by: martinsmith
2 Replies

3. Shell Programming and Scripting

Perl - line count of a file

Hi, I am quite new to perl scripting. I have a dat file (datFile) from which I am pulling only first column and saving the output to a new file (f). From that file (f) I am removing blank lines and saving it to new file (datFile1). I am able to get the count of $f file in variable $cnt. But... (4 Replies)
Discussion started by: Neethu
4 Replies

4. Shell Programming and Scripting

Need perl or shell script to sort vertical lines to horizontal line in csv format

Need perl or shell script to sort vertical lines to horizontal line in csv format My file like below ------------------------- ================================================================================ PATH PINKY1000#I1-1-ZENTA1000-2#I7-1-ASON-SBR-UP-943113845 ... (4 Replies)
Discussion started by: sreedhargouda.h
4 Replies

5. Shell Programming and Scripting

PERL foreach & open not working together

My script is as below: my $tile_list = `egrep "FCFP_TILE_LIST.*=" ${BudgetDir}/tile.params | sed -e 's/FCFP_TILE_LIST//' | sed -e 's/=//'`; print "Tile List = ".$tile_list."\n"; my @tiles = split(/\s+/, $tile_list); $unconst_out = "${DestDir}/Unconstrained_ports.rpt"; $check_tim_out =... (2 Replies)
Discussion started by: kuchi7
2 Replies

6. Shell Programming and Scripting

awk: sort lines by count of a character or string in a line

I want to sort lines by how many times a string occurs in each line (the most times first). I know how to do this in two passes (add a count field in the first pass then sort on it in the second pass). However, can it be done more optimally with a single AWK command? My AWK has improved... (11 Replies)
Discussion started by: Michael Stora
11 Replies

7. Programming

PERL, search and replace inside foreach loop

Hello All, Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location. CODE: .. .. ... foreach $key(keys %arr_hash) { my ($loc,$ind,$add) = split /,/, $arr_hash{$key}; &create_verilog($key, $loc, $ind ,$add); } sub create_verilog{... (2 Replies)
Discussion started by: riyasnr007
2 Replies

8. Shell Programming and Scripting

How to count using foreach

I have a simple csh script that has a simple foreach loop that goes over numbers, from 1 to 10: foreach n(1 2 3 4 5 6 7 8 9 10) ... end Now I want to expand the script to work on over a hundred consecutive n values. Obviously, typing all the numbers between 1 to 100 is an unreasonable... (7 Replies)
Discussion started by: mcbenus
7 Replies

9. Shell Programming and Scripting

Perl line count if it matches a pattern

#!/usr/bin/perl use Shell; open THEFILE, "C:\galileo_integration.txt" || die "Couldnt open the file!"; @wholeThing = <THEFILE>; close THEFILE; foreach $line (@wholeThing){ if ($line =~ m/\\0$/){ @nextThing = $line; if ($line =~ s/\\0/\\LATEST/g){ @otherThing =... (2 Replies)
Discussion started by: nmattam
2 Replies

10. Shell Programming and Scripting

Nested foreach in perl

I have two arrays @nextArray contains some files like \main\1\Xul.xml@@\main\galileo_integration_sjc\0 \main\1\PortToStorageDialog.xml@@\main\galileo_integration_sjc\0 . . . \main\1\PreferencesDialog.xml@@\main\galileo_integration_sjc\0 @otherArray contains some files like ... (2 Replies)
Discussion started by: nmattam
2 Replies
Login or Register to Ask a Question