473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to sort the data?

89
Hi I have one row of data from a database as @data.

I need to sort the items available in $data[1], 2,3,4,5,6,7,8 positions

For example if the data are as below:

man, women, man, man, women, man, man, man in their respective position. Then I want my output to be as below:

134678: man
25: woman


How do I do this? I am not so familiar with perl hash as a beginner. It would be of great help if someone helps me out.
Thanks and Regards
Jul 15 '08 #1
5 1258
nithinpes
410 Expert 256MB
You have to create a hash from the array with each unique element as key. Then parse the array and concatenate the position value for each element matching the key, as follows:

Expand|Select|Wrap|Line Numbers
  1. @data=(man, women, man, man, women, man, man, man);
  2.  
  3. foreach(@data) {
  4.  $entry{$_}++; 
  5.  #hash with each unique data as key & number of occurence as its value
  6. }
  7.  
  8. foreach my $key (sort keys %entry) {
  9.  my $i=1;
  10.  my $str;
  11.   foreach(@data) {
  12.   $str.="$i" if(/$key/); #concatenate the position 
  13.   $i++;
  14. }
  15.  print "$str: $key\n";
  16. }
  17.  
Jul 15 '08 #2
KevinADC
4,059 Expert 2GB
another way using a hash of arrays:

Expand|Select|Wrap|Line Numbers
  1. @data = qw(man women man man women man man man);
  2. my %hash;
  3. my $i;
  4. foreach my $thing (@data) {
  5.    push @{$hash{$thing}},$i++;
  6. }
  7. for (keys %hash) {
  8.    print "$_:", join(',',@{  $hash{$_} }), "\n"; 
Jul 15 '08 #3
Hi Lilly,


Good Luck,

Write me for any help in C, C++, Perl, PHP and UNIX related stuff. I will answer you with in the time with free of cost.

Rammohan Alampally,
HP Technologies
Bangalore
Jul 16 '08 #4
jain236
36
Hi Lilly,


Good Luck,

Write me for any help in C, C++, Perl, PHP and UNIX related stuff. I will answer you with in the time with free of cost.


Rammohan Alampally,
HP Technologies
Bangalore

Hi ramohna may a simple way, no to complicate life with hasesh
Expand|Select|Wrap|Line Numbers
  1. @data=('man', 'women', 'man', 'man',
  2.        'women', 'man', 'man', 'man');
  3. my $str;
  4. my $women;
  5. for ($i = 1;$i <=$#data+1;$i++)
  6. {
  7.   $str.= $i if $data[$i-1]=~ /man/;
  8.    $women.= $i if $data[$i-1]=~ /women/;
  9.  
  10. }
  11. print "$str:man\n";
  12. print "$women:women";
  13.  
Jul 16 '08 #5
lilly07
89
Thanks for the help.
Jul 16 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
1
by: Michael Hill | last post by:
I have a section of a stylesheet (below) I am trying to configure. The part I am having trouble with is the when test. I have a series of pulldowns that I can select from, some are text and some...
3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
4
by: t3chn0n3rd | last post by:
Do you think it is relatively easy to write sort algorithms such as the common Bubble sort in Python as compared to other high level programming langauges
0
by: kronus | last post by:
Hi everyone, Please tell me what I am doing wrong: I have an xml data coming to my Air application that looks something like this: <project name="Sales Demos" path="/Company Home/Client...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.