473,386 Members | 1,720 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,386 software developers and data experts.

Sort function

43
Hi,

I'm trying to use the sort function without success up to now. I'm opening a file and wish to reverse sort on the first field "$users_fields[0]" and then print the output found in the IF statement.

Can you help?

Thanks,

Here is the code, which print the entire file not sorted:

Expand|Select|Wrap|Line Numbers
  1. open (VENTE_DATABASE, "<$path/data/a_a_vendre.data");
  2.  
  3.  while (<VENTE_DATABASE>) {
  4.   $primeurs = $_;
  5.    chop $primeurs;
  6.    @users_fields = split (/\|/, $primeurs);
  7.  
  8.  if ($users_fields[10] > "0") {    print  "$users_fields[10]<br> $users_fields[24]<br> $users_fields[16], $users_fields[15], $users_fields[14]<br> $users_fields[28] $users_fields[143]<p>" ;  }
  9.  
  10.  }   # End of while (<VENTE_DATABASE>)
  11.  
  12. close (VENTE_DATABASE);
Jun 2 '11 #1
6 1339
miller
1,089 Expert 1GB
A few thoughts concerning your script first. You should always use chomp instead of chop unless there's a specific reason why you need chop.

Second, you really need use strict; and use warnings; Yes it requires a little more time at the start, but it's always worth the investment.

Anyway, if you want to sort your results, you'll need to cache them so you can apply sort at the end. Here's how I would modify your script:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $vdfile = "$path/data/a_a_vendre.data";
  5.  
  6. open my $fh, $vd or die "Can't open $vdfile: $!";
  7.  
  8. my @matched;
  9.  
  10. while (<$fh>) {
  11.     chomp;
  12.     my @users_fields = split /\|/;
  13.  
  14.     if ($users_fields[10] > "0") {
  15.         push @matched, \@users_fields;
  16.     }
  17. }   # End of while (<$fh>)
  18.  
  19. close $fh;
  20.  
  21. for (sort {$a->[0] <=> $b->[0]} @matched) {
  22.     print  <<"END_PRINT";
  23. <p>$_->[10]<br>
  24. $_->[24]<br>
  25. $_->[16], $_->[15], $_->[14]<br>
  26. $_->[28] $_->[143]</p>
  27. END_PRINT
  28.  
Note, concerning your html, it looks like your using <p> to denote the end of a paragraph. This isn't proper. Instead use <p> ... </p> to enclose the paragraph, and you can use <br> within those tags for extra formatting.

- Miller
Jun 2 '11 #2
yjulien
43
Thanks again Miller !

I did not use strick and warning because this script is already part of a much bigger one.

I will try this and let you know.

Thanks you again.

yves
Jun 3 '11 #3
yjulien
43
Hi again Miller,

You wrote :
Expand|Select|Wrap|Line Numbers
  1. my $vdfile = "$path/data/a_a_vendre.data";
  2.  
  3. open my $fh, $vd or die "Can't open $vdfile: $!"; 
  4.  
Should it not be:
Expand|Select|Wrap|Line Numbers
  1. my $vdfile = "$path/data/a_a_vendre.data";
  2.  
  3. open my $fh, $vdfile or die "Can't open $vdfile: $!"; 
  4.  
Jun 3 '11 #4
yjulien
43
Hi Miller,

I got the code working. There was a " } " missing at the end.

One more question, how can we sort on more then 1 field? Say I want to sort on field $a->[0] & on field $a->[1]? Is it possible to double sort without modifying the code too much? Is there some kind of loop within a loop strategy possible?

Thanks,

Yves
Jun 3 '11 #5
yjulien
43
@yjulien
Never mind Miller, I found it! :D

Expand|Select|Wrap|Line Numbers
  1. for (sort {$b->[11] <=> $a->[11] or $b->[0] <=> $a->[0]} @matched) {
  2.     print  <<"END_PRINT";
  3. <p>$_->[10]<br>
  4. $_->[24]<br>
  5. $_->[16], $_->[15], $_->[14]<br>
  6. $_->[28] $_->[143]</p>
  7. END_PRINT
Quite neat and simple!
Jun 3 '11 #6
miller
1,089 Expert 1GB
Good job mate, that looks right.

Sorry I didn't respond to your earlier posts, but I was on a vacation myself.

Until next time,
- Miller
Jun 6 '11 #7

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

Similar topics

1
by: Shaunak Kashyap | last post by:
Does anyone know what sorting algorithm(s) -- quicksort, mergesort, radix sort, etc. -- does PHP use internally in its sort function?
2
by: Ken R. | last post by:
Hello all, I am relatively new to python but I am having an issue with custom sort functions.. I am trying to sort a list of lists or tuples with arbitrary ascending or descending sorts. For...
2
by: Fawke101 | last post by:
Hi there, I have a JS/ASP application that sorts a SQL populated table. This works great, and sorts the data fine, apart from my currency columns. It wants to read the data as text as far as i...
2
by: Rachel Forder | last post by:
Hi All, I have a problem related to the sort function provided by STL. class A{ A(string, string, int); string itemA; string itemB; int itemC; };
7
by: Pat | last post by:
class data { public: double time; // other functions.... } I want to put a lot of "data" classes into a container, and the data order should be sorted according to "time" variable. One of...
5
by: SteM | last post by:
Hi!! I need something like a "sortfunction" because i want to sort the files in a directory x by name or date. i can't find any system function in c++ - any ideas, examples?? Thx, SteM
5
by: GIMME | last post by:
My question is ... How do I sort an Array on numeric, not character values ? In the example below, after sorting the contents are 1,10,2,3 . How do I get the contents to be 1,2,3,10 ? ...
3
by: rwise5 | last post by:
I have been tasked with finishing the following C program. I need to develop the sort by birthday function and the print grade function. I have been working on the print function for the last week...
0
by: =?iso-8859-1?Q?=22Orlando_D=F6hring=22?= | last post by:
Dear community, I want to use the sort function to sort a (nested) list. General information can be found below. http://www.python.org/doc/2.4.2/lib/typesseq-mutable.html...
2
by: calred | last post by:
i want to create a function that does the same as the build-in function .sort(), but it is more difficult than i think. my function is as below: ------------------------------------- def...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.