By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,115 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,115 IT Pros & Developers. It's quick & easy.

plzzzzzz help me in writting a code. i am beginer in Perl

P: 1
Hello Everyone,
I need to read data from a CVS file(i created it from micosoft excel) and then need to match it with the one of the date from the command line.If the date is there then it should say yes or else it should say no. I am not getting how to do this as i am just a beginer in perl. Can anybody help me in writting the code.
the files r toooo.. big to be attached , so i have just shown a tiny portion of it.
thanks in advance,
rimjim

example: The data i'll be needing will be 15 next enteries from the date
5/7/1996
i.e i'll start looking for 5/8/1996 if not found then i'll go for 05/09/1996 till u get that date into ur cvs data. But luckily i have 5/8/1996 into my cvs data. So now i need to take the mean of 15 data values i.e will values will be after the second comma i.e

0.017645172
-0.00549951..............so on till 0.001934775.

15 values taken from cvs file

5/8/1996,570.96,0.017645172
5/9/1996,567.82,-0.00549951
5/10/1996,570.28,0.004332359
5/13/1996,578.18,0.013852844
5/14/1996,584.18,0.010377391
5/15/1996,599.03,0.025420247
5/16/1996,599.27,0.000400648
5/17/1996,610.01,0.017921805
5/20/1996,615.05,0.00826216
5/21/1996,612.66,-0.003885863
5/22/1996,613.13,0.000767147
5/23/1996,598.8,-0.023371879
5/24/1996,606.89,0.013510354
5/27/1996,609.89,0.004943235
5/28/1996,611.07,0.001934775

cvs file data is as follows and after that i also have the file from where the initial dates come from. i want someone who is good at perl, plzzz help me in writing a code or if u can write a code for me.

MERVAL Index,,Daily Return
Date,Px Last,

5/6/1996,554.44,0.009118541
5/7/1996,561.06,0.011939975
5/8/1996,570.96,0.017645172
5/9/1996,567.82,-0.00549951
5/10/1996,570.28,0.004332359
5/13/1996,578.18,0.013852844
5/14/1996,584.18,0.010377391
5/15/1996,599.03,0.025420247
5/16/1996,599.27,0.000400648
5/17/1996,610.01,0.017921805
5/20/1996,615.05,0.00826216
5/21/1996,612.66,-0.003885863
5/22/1996,613.13,0.000767147
5/23/1996,598.8,-0.023371879
5/24/1996,606.89,0.013510354
5/27/1996,609.89,0.004943235
5/28/1996,611.07,0.001934775
5/29/1996,599.32,-0.019228566
5/30/1996,603.79,0.007458453
5/31/1996,600.44,-0.005548287
12/4/1996,628.76,-0.010839298
12/24/1996,642.11,0.006032025
12/26/1996,650.04,0.012349909
9/2/1997,826.83,0.020714771
9/3/1997,834.18,0.008889373

Date's are taken from this file

,,,,,Volatility 1,,,Volatility 2,,,Volatility 3,,,Volatility 4,
Adj_issue_date,Country,Year,Issuer,Mean of 15-day market index return,Median of 15-day market index return,Volatility,Mean of 15-day market index return,Median of 15-day market index return,Volatility,Mean of 15-day market index return,Median of 15-day market index return,Volatility,Mean of 15-day market index return,Median of 15-day market index return,Volatility

05/07/96,Argentina,1996,Siderar SAIC,,,,,,,,,,,,
09/26/96,Australia,1996,Southern Star Group Ltd
10/10/96,Australia,1996,Buka Minerals Ltd
10/29/96,Australia,1996,Red Back Mining NL
11/15/96,Australia,1996,RCR Engineering Ltd
11/20/96,Australia,1996,Minerals Corp Ltd
11/21/96,Australia,1996,Cardia Mining NL
Oct 19 '06 #1
Share this Question
Share on Google+
1 Reply

miller
Expert 100+
P: 1,089
All you need is the Text::CSV cpan module. The below code takes a date from the command line of the format "m/d/YYYY" and scans the csv file "20061023csv.csv". I assumed that the date was of the given format, as it provided an easy way for testing if we had reached the target date time. But if it is possible that the date comes in alternate formats, you can use the Date::Parse module to translate the dates into a comparable format.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. require Text::CSV;
  6.  
  7. my $inFile = '20061023csv.csv';
  8. my $targetDate = shift || '5/7/1996';
  9. my $targetCount = 15;
  10.  
  11. open(IN, $inFile) or die "open $inFile: $!";
  12.  
  13. my $csv = Text::CSV->new;
  14.  
  15. my $sortDate = sprintf('%3$04d%1$02d%2$02d', split '/', $targetDate);
  16.  
  17. my @data = ();
  18.  
  19. while (my $line = <IN>) {
  20.     if ($csv->parse($line)) {
  21.         my @field = $csv->fields;
  22.         my $date = sprintf('%3$04d%1$02d%2$02d', split '/', $field[0]);
  23.  
  24.         if ($date > $sortDate) {
  25.             push @data, \@field;
  26.         }
  27.  
  28.         if (@data == $targetCount) {
  29.             # Reached our target
  30.             last;
  31.         }
  32.     } else {
  33.         print "parse() failed on argument: ", $csv->error_input, "\n";
  34.     }
  35. }
  36.  
  37. close(IN) or die "close $inFile: $!";
  38.  
  39. if (@data == 0) {
  40.     print "no records found for $targetDate\n";
  41.     exit;
  42. }
  43.  
  44. my $sum = 0;
  45. foreach my $record (@data) {
  46.     $sum += $record->[2];
  47. }
  48. my $average = $sum / @data;
  49.  
  50. print "average of " . @data . " records is ". $average . " from " . $data[0][0] . " to " . $data[-1][0] . "\n";
  51.  
  52. 1;
  53.  
  54. __END__
  55.  
Oct 23 '06 #2

Post your reply

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