Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: performing calculation in cells in a file

by FunkyMonk (Chancellor)
on Aug 22, 2007 at 15:18 UTC ( #634388=note: print w/replies, xml ) Need Help??


in reply to RE: performing calculation in cells in a file

This does most of the work for you, but I've left some things for you to do:

<DATA>; my @prev_line; while ( <DATA> ) { my @this_line = split /[,\s]+/; shift @this_line; print join( ", ", map { "$this_line[$_]-$prev_line[$_]" } 0 .. $#t +his_line ), "\n" if @prev_line; @prev_line = @this_line; } __DATA__ date, val1, val2, val3, val4 1/2/2007, 1, 4, 5, 6 1/3/2007, 2, 5, 7, 10 1/5/2007, 5, 6, 8, 11

Output:

2-1, 5-4, 7-5, 10-6 5-2, 6-5, 8-7, 11-10

You may find this version easier to follow (using the same __DATA__ as earlier):

<DATA>; my @prev_line; while ( <DATA> ) { my @this_line = split /[,\s]+/; shift @this_line; if ( @prev_line ) { my @output; for ( 0 .. $#this_line ) { push @output, "$this_line[$_]-$prev_line[$_]"; } print join( ", ", @output ), "\n"; } @prev_line = @this_line; }

That leaves you with the job and handling the files.

I'm sure google will help, and there's always the Perl documentation.

update: omitted __DATA__ first time round
update^2: added second example

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://634388]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2022-01-21 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (58 votes). Check out past polls.

    Notices?