473,474 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to sort csv file with perl

I haven't touched Perl in a while but I ran into a relatively simple parsing problem at work that it would be perfect for. I threw together a program using what I remembered--just want to get comments and see if I'm on the right track:

Data looks like this:
Expand|Select|Wrap|Line Numbers
  1. 2010-10-05 09:00:00-1000,1,1012,,
  2. 2010-10-05 09:00:00-1000,2,397,,
  3. 2010-10-05 09:00:00-1000,3,0,,
  4. 2010-10-05 09:00:00-1000,4,165,,
  5.  
But what I want is <DT>,1012,397,0,165,,

Pseudocode:
Expand|Select|Wrap|Line Numbers
  1. Open the dang file
  2. Remove trailing comma from each line
  3. Remove date from every n+1,n+2,n+3 lines
  4. Remove (1,2,3,4) from every n+1,n+2,n+3 lines
  5. Take final values from every n+1,n+2,n+3 lines and push it onto the end of every n line
  6. Replace trailing comma
  7. Close the dang file
  8.  
So what I want to do is open the file, put it into an array, chomp and chop it, split that array into other arrays, loop through and rewrite each array line with the right subcomponents of each array, offset?

Trial code:
Expand|Select|Wrap|Line Numbers
  1. open (DATA,$ARGV[0]); or
  2.     die("File '$ARGV[0]' does not exist?\n");
  3. my @lines = <DATA>;
  4. close DATA or 
  5.     warn $! ? "Error closing pipe: $!"
  6.          "Exit status $? from sort";
  7.  
  8.     chomp @lines;
  9.     / get rid of the second comma after each line /
  10.     chop @lines;
  11.  
  12.     / break up fields into categories by comma /
  13.     (@time,@mtu,@watth) = split "," , @lines;
  14.  
  15.     / move every n+2nd, n+3rd, and n+4th power value up to the nth line /
  16.     $hop = 0;
  17.     while(@lines) {
  18.     @lines[$hop] = @time[$hop],@watth[$hop],@watth[$hop+1],@watth[$hop+2],@watth[$hop+3]",,";
  19.  
  20.     / hop 4 at a time /
  21.     $i+=4;
  22.     }
  23. open (OUT,$ARGV[0]);
  24.  
  25. print OUT @lines;
  26.  
Thanks for your help. I would love to get competent with Perl.
Nov 25 '10 #1
0 1353

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

Similar topics

2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
2
by: Xah Lee | last post by:
Today we'll write a program that can sort a matrix in all possible ways. Here's the Perl documentation. I'll post a Perl and Python version in 2 days. ----------- sort_matrix( $matrix, , ,...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
4
by: Radek G. | last post by:
Hello I must implemend sorting in perl (that's not too dificult;) ) but...i must sort some array with grouping (like in sql). I meen... For Example I have array with first and last names:...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
6
by: bcochofel | last post by:
I'm using xsl to list an xml file that contains something like: sites, tag and weight. I'm listing this in a table with the following titles: | URL | TAG | WEIGHT (each title his a link) What...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
6
by: gopalsd | last post by:
Hi Friends, Can anyone pls help me to resolve my school test in PERL................ as follows..... #!/usr/bin/perl @data=N; $sum=0; print"enter the required No. of numbers to be inputed...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
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,...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.