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

perform calculations on cells in csv file

there is a csv file like this:
date, val1, val2, val3, val4 etc
1/2/2007, 1, 4, 5, 6
1/3/2007, 2, 5, 7, 10
1/5/2007, 5, 6, 8, 11

what I need to do is, calculate the differnce between cells for each column and write it to a file. for example, since there is no previous value to row one, write first row to a file. then subtrack second raw values from the first row values and write the output to the second line in ouptput file - (2-1, 5-4, 7-5, 10, 6). Third raw in the output file should be, 5-2, 6-5, 8-7, 11-10 and so on. Can anybody help me with this?
Aug 22 '07 #1
7 2040
KevinADC
4,059 Expert 2GB
Please post what you have tried so far.
Aug 22 '07 #2
I dont even know how to start this
Aug 22 '07 #3
numberwhun
3,509 Expert Mod 2GB
Well, if that is the case, I would have to ask how much Perl experience do you have? Do you know how to work with modules?

I am asking because there is a perl module that falls under the DBI module family called DBD::CSV, that allows you to interact with a CSV file as if it were a database. Take a read of that page to find out about it.

If you don't have much Perl experience, I would definitely run down to your local Barnes & Noble and pick up "Learning Perl, 4th Edition" as it is a great starter book.

Regards,

Jeff
Aug 22 '07 #4
I have little experience with perl. I was thinking about making an array, read the lines into an array and try to subtrack the numbers like that but it may need to be two dimensional arrays.
Aug 22 '07 #5
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use Data::Dumper;
  4.  
  5. open(FILE,"<test.csv");
  6. $row=-1;$col=-1;
  7. while (<FILE>) {
  8.     chomp;
  9.     if (/^[0-9]/) {
  10.         chomp;
  11.         $row++;
  12.         $col = -1;
  13.  
  14.         foreach $char (split /,/) {
  15.             $col++;
  16.             $cell[$row][$col]=$char;
  17.         }
  18.     }
  19. }
  20.  
  21. for $i (0..$#cell) {
  22.     $aref = $cell[$i];
  23.     $n = @$aref - 1;
  24.     for $j (0..$n) {
  25.         print "$cell[$i][$j],";
  26.     }
  27.     print "\n";
  28. }
  29.  
basically, I have the information in the two dimensional array now. what I need to do is to the subtrack the symetic cells from each other. First column is the time stamp and needs to be left alone.
Aug 22 '07 #6
there is a csv file like this:
date, val1, val2, val3, val4 etc
1/2/2007, 1, 4, 5, 6
1/3/2007, 2, 5, 7, 10
1/5/2007, 5, 6, 8, 11

what I need to do is, calculate the differnce between cells for each column and write it to a file. for example, since there is no previous value to row one, write first row to a file. then subtrack second raw values from the first row values and write the output to the second line in ouptput file - (2-1, 5-4, 7-5, 10, 6). Third raw in the output file should be, 5-2, 6-5, 8-7, 11-10 and so on. Can anybody help me with this?

I have this code so far.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl 
  2. use Data::Dumper; 
  3. open(FILE,"<test.csv"); 
  4.  
  5. $row=-1;$col=-1; 
  6. while (<FILE>) { 
  7.     chomp; 
  8.     if (/^[0-9]/) { 
  9.         chomp; 
  10.         $row++; 
  11.         $col = -1; 
  12.         foreach $char (split /,/) { 
  13.             $col++; 
  14.             $cell[$row][$col]=$char; 
  15.         } 
  16.     } 
  17.  
  18. for $i (0..$#cell) { 
  19.     $aref = $cell[$i]; 
  20.     $n = @$aref - 1; 
  21.     for $j (0..$n) { 
  22.         print "$cell[$i][$j],"; 
  23.     } 
  24.     print "\n"; 
  25. }
  26.  
Aug 22 '07 #7
miller
1,089 Expert 1GB
Please do not double post your question. I've merged the two threads for you.

- MODERATOR
Aug 22 '07 #8

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

Similar topics

1
by: Job Lot | last post by:
I have to create a loan (Line of Credit) tracker application, where users can analyse their loan over a term. They can make changes to monthly entries to see how long it would take them to pay of the...
3
by: Mika | last post by:
Is it possible to perform functions on a recordset (rather than on the source database), e.g. COUNT, MAX etc The reason I would like to do this is because I have a hugely complex view which...
5
by: mrid via DotNetMonster.com | last post by:
hi. im exporting data from a vb form to excel. i am able to create a new excel file, save and edit it without any trouble, but the formatting is giving me hell! i need to be able to show certain...
2
by: murdock | last post by:
Is there a way to perform functions upon databound data that is to be used in a GridView? For example, in the following code where I am using a GridView to display a resulting asp:SqlDataSource ...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
0
by: Bails | last post by:
Hi, Im in the process of building my VERY first VB .Net Express app in the form of a Poker Tournament timer. I have the basics up and running ( thanks Tom), but want to add some extra features....
4
by: wrldruler | last post by:
Hello, First, I know it's against "Access Law" to save calculations in a table, but....I want/need to. I currently have sub-totals being calculated inside a form, using DMax, DCount, and...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
2
by: Blacky | last post by:
Hi, I am using c# asp.net application.I have datagrid which binds column dynamically and i make certain columns say visibility to false in my itemdatabound event EMPID AS E.ITEM.CELLS.VISIBLE =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
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...

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.