Sponsored Content
Top Forums Shell Programming and Scripting Perl - Sort and Count foreach line Post 302518162 by sureshcisco on Thursday 28th of April 2011 06:22:22 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
gdeskcal(1)						      General Commands Manual						       gdeskcal(1)

NAME
gdeskcal - A cute little eye-candy calendar for your desktop. SYNOPSIS
gdeskcal [ options ] DESCRIPTION
gdeskcal is a cute little eye-candy calendar for your desktop. It features transparency with smooth alpha-blending and its appearance can be changed completely by using skins. OPTIONS
gdeskcal accepts the following options: --help Displays help about the command line options and exits. --dontsave Tells 'gDeskCal' not to save any configuration changes on exit. --dpi=<resolution> Tells 'gDeskCal' that the screen resolution is <resolution> dots per inch. You normally don't have to use this option since 'gDeskCal' can guess the value. This option affects the scaling of fonts. --opaque Causes the calendar to be opaque so you cannot see the background through it. This setting will not be saved. --skin=<skin> The name of the skin to use. A skin's name is the name of its directory. -x <value> The x coordinate. -y <value> The y coordinate. AUTHOR
gdeskcal was written by Martin Grimme <martin@pycage.de>. This manual page was written by Sebastien Bacher <seb128@debian.org>, for the Debian GNU/Linux system (but may be used by others). February 24, 2003 gdeskcal(1)
All times are GMT -4. The time now is 03:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy