473,320 Members | 2,110 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,320 software developers and data experts.

CSV File - Sort data by second field

How do I sort the contents of a flat file based on the second field?
What I have now sorts by the first field - 0

Expand|Select|Wrap|Line Numbers
  1. open (BASE, $dbk) || do {&no_open;};
  2. @sorted = sort(<BASE>);
  3. foreach $pair (@sorted) {
  4.     @show = split(/,/, $pair);
  5.  
  6.     close(BASE);
  7.  
My fields are:
[0,1,2,3,4,5,6,7,8,9,10,11]

many thanks
Paul
Jul 27 '07 #1
7 9374
I also tried this...

Expand|Select|Wrap|Line Numbers
  1. open (BASE, $db) || do {&no_open;};
  2. print BASE "$snumber,$kit,$id,$item_name,$in_out,$itemd_name,$date_out,$date_in,$name_out,$staff_out,$staff_in";
  3. @sorted = <BASE>;
  4.  
  5. foreach $line (sort secondfield @sorted) {
  6.     @show = split(/,/,"$line");
  7.     sub secondfield {
  8.         ($snumber,$kit,$id,$item_name,$in_out,$itemd_name,$date_out,$date_in,$name_out,$staff_out,$staff_in)= split(/,/,$a);
  9.         ($snumber2,$kit2,$id2,$item_name2,$in_out2,$itemd_name2,$date_out2,$date_in2,$name_out2,$staff_out2,$staff_in2)= split(/,/,$b);
  10.         return ($kit2 <=> $kit);
  11.     }
  12.     $show[0] etc
  13.  
  14.     close (BASE);
  15. }
  16.  
but it still sorts by the first field.

thanks
Jul 27 '07 #2
miller
1,089 Expert 1GB
You must first parse the contents of your file so that perl can do the sorting on the field. There is no magic available here. You must program it all

Expand|Select|Wrap|Line Numbers
  1. my @data;
  2. open(BASE, $db) or die "Can't open $db: $!";
  3. while (<BASE>) {
  4.     chomp;
  5.     push @data, [split ',', $_];
  6. }
  7.  
And then you can sort. The sort documentation itself will tell you how to do this.

http://perldoc.perl.org/functions/sort.html

Expand|Select|Wrap|Line Numbers
  1. my @sorted = sort {$a->[1] <=> $b->[1]} @data;
  2.  
- Miller
Jul 27 '07 #3
numberwhun
3,509 Expert Mod 2GB
Its so funny, but that is exactly what I was thinking and working on for him, I just got caught up in work coding. Thanks for that confirmation Miller!

Jeff
Jul 27 '07 #4
Great!...thanks a heap
Jul 27 '07 #5
KevinADC
4,059 Expert 2GB
You must first parse the contents of your file so that perl can do the sorting on the field. There is no magic available here. You must program it all

Expand|Select|Wrap|Line Numbers
  1. my @data;
  2. open(BASE, $db) or die "Can't open $db: $!";
  3. while (<BASE>) {
  4.     chomp;
  5.     push @data, [split ',', $_]
  6. }
  7.  
And then you can sort. The sort documentation itself will tell you how to do this.

http://perldoc.perl.org/functions/sort.html

Expand|Select|Wrap|Line Numbers
  1. my @sorted = sort {$a->[1] <=> $b->[1]} @data;
  2.  
- Miller

There is a module or two that will handle the backend stuff of sorting mixed data in delimited fields. But really it seems so much easier to just write your own sorting function than to use a module unless a person just can't grasp how the sort() function works.
Jul 27 '07 #6
miller
1,089 Expert 1GB
It doesn't surprise me that there would be such modules. Any ones in particular you were thinking of?

I also considered using Text::CSV_XS just to demonstrate how one would, but it felt like overkill given the data as he expressed it. Must better to just focus the problem at sorting complex data structures.

- Miller
Jul 27 '07 #7
KevinADC
4,059 Expert 2GB
It doesn't surprise me that there would be such modules. Any ones in particular you were thinking of?


- Miller
No, none in particular.
Jul 28 '07 #8

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...
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
11
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
2
by: mael.iosa | last post by:
Hi, I'm new to this group and fairly new to Access. I have a bunch of data, and after several other queries, I generate the following query which has two fields: Bin, Time 20 3.5 20 3.9 20...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
8
blazedaces
by: blazedaces | last post by:
So I have a program below which writes an excel file with multiple sheets based on inputs of sheet names, data, cell types, etc. It uses Apache POI, which is currently the only thing I found...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.