473,657 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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.0 17645172
5/9/1996,567.82,-0.00549951
5/10/1996,570.28,0.0 04332359
5/13/1996,578.18,0.0 13852844
5/14/1996,584.18,0.0 10377391
5/15/1996,599.03,0.0 25420247
5/16/1996,599.27,0.0 00400648
5/17/1996,610.01,0.0 17921805
5/20/1996,615.05,0.0 0826216
5/21/1996,612.66,-0.003885863
5/22/1996,613.13,0.0 00767147
5/23/1996,598.8,-0.023371879
5/24/1996,606.89,0.0 13510354
5/27/1996,609.89,0.0 04943235
5/28/1996,611.07,0.0 01934775

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.0 09118541
5/7/1996,561.06,0.0 11939975
5/8/1996,570.96,0.0 17645172
5/9/1996,567.82,-0.00549951
5/10/1996,570.28,0.0 04332359
5/13/1996,578.18,0.0 13852844
5/14/1996,584.18,0.0 10377391
5/15/1996,599.03,0.0 25420247
5/16/1996,599.27,0.0 00400648
5/17/1996,610.01,0.0 17921805
5/20/1996,615.05,0.0 0826216
5/21/1996,612.66,-0.003885863
5/22/1996,613.13,0.0 00767147
5/23/1996,598.8,-0.023371879
5/24/1996,606.89,0.0 13510354
5/27/1996,609.89,0.0 04943235
5/28/1996,611.07,0.0 01934775
5/29/1996,599.32,-0.019228566
5/30/1996,603.79,0.0 07458453
5/31/1996,600.44,-0.005548287
12/4/1996,628.76,-0.010839298
12/24/1996,642.11,0.0 06032025
12/26/1996,650.04,0.0 12349909
9/2/1997,826.83,0.0 20714771
9/3/1997,834.18,0.0 08889373

Date's are taken from this file

,,,,,Volatility 1,,,Volatility 2,,,Volatility 3,,,Volatility 4,
Adj_issue_date, Country,Year,Is suer,Mean of 15-day market index return,Median of 15-day market index return,Volatili ty,Mean of 15-day market index return,Median of 15-day market index return,Volatili ty,Mean of 15-day market index return,Median of 15-day market index return,Volatili ty,Mean of 15-day market index return,Median of 15-day market index return,Volatili ty

05/07/96,Argentina,19 96,Siderar SAIC,,,,,,,,,,, ,
09/26/96,Australia,19 96,Southern Star Group Ltd
10/10/96,Australia,19 96,Buka Minerals Ltd
10/29/96,Australia,19 96,Red Back Mining NL
11/15/96,Australia,19 96,RCR Engineering Ltd
11/20/96,Australia,19 96,Minerals Corp Ltd
11/21/96,Australia,19 96,Cardia Mining NL
Oct 19 '06 #1
1 1549
miller
1,089 Recognized Expert Top Contributor
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.cs v". 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

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

Similar topics

0
1118
by: Hai Nguyen | last post by:
Hi everyone I'm writting a javascript for my validators. Some of them will be validated or server side, some of them will be validated on client side. Since I don't want to mix my javascript with my aspx I want to split them into 2 files and linke them together. I don't know if there is a decent way in visual studio. net can help me to write code faster. The reason i say "write faster" since if I write javascript in aspx, with some...
6
1466
by: noridotjabi | last post by:
Is there any way in C to write source which would make an executable that excepted modifications. For instance say we have modable program that excepts plugins. That starts with a menu. 1)Add 2)Subtract 0)Add Mod :: When entering zero: Mod Source ::(whatever.c or maybe .o would be better) Say the mod we put in was to add a divide option to the main menu.
5
3219
by: hn.ft.pris | last post by:
Hi: I'm a beginer of STL, and I'm wondering why none of below works: ######################################################################## .......... string str("string"); if ( str == "s" ) cout << "First character is s" << endl; OR: string str("string"); string::iterator it = str.begin();
6
9221
by: Java1963 | last post by:
Need help with writting an application that prompt for and read a double value representing a monetary amount. -------------------------------------------------------------------------------- Hello all, I am new to this Java stuff and, I need help in writting an application will prompt for and read a double value representing a monetary amount.Then determine the fewest numbers of each bill and coin needed to represent that amount,...
1
2317
by: rahulbsbs | last post by:
Sir how can i get the ip address of the remote system by writting a java code,ie i want to get ip address of the remote system that is connected to my server computer ,the ip address must be obtained by writting a java program.Is there any class for that.Please give me an example for getting this.
1
1239
by: hamed steph | last post by:
i'm a beginer in programing and i need very much your help .i need explanation about while statement (loop initialization and structure of while ) also qualifier (long,short,unsigned ,signed,) type of variable (character and double). think you.......
5
2314
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just the number. whats wrong with my code? #!/usr/bin/perl use Fcntl; @array = (a..z); srand; foreach (1..5) { $name = int(rand scalar(@array));
37
1832
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 --srcport 131.188.37.59 --destaddress 1398 --destport tcp --protocol
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8499
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.