Current location: Hot Scripts Forums » Programming Languages » Perl » File Handling


File Handling

Reply
  #1 (permalink)  
Old 03-21-06, 04:19 AM
spunk spunk is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
File Handling

Hi all.

I'm currently working with a huge text file (over 2Mb and 34,000 lines of text) that are separated by columns (probably using a /t special char). I want to calculate some values in those columns.

Example:

Column 1 refers to Height, #2 to Pressure and #3 to Temperature. I know that in perl it is possible to go through the file and search certain values in those columns (i.e.: the highest value, calculate average, etc.). I just want to know how do I handle such a file using perl.

Thank you in advance.

Hugo
Reply With Quote
  #2 (permalink)  
Old 03-21-06, 02:18 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
I wouldn't say 2mb is a huge file, certainly not small but most any computer can handle this amount of data easily.

You want to look into using open() to open and read the file and split() to split the lines into the seperate columns, and most likely using a hash to build a dataset out of the file and then run comparisons to get the results you want.
Reply With Quote
  #3 (permalink)  
Old 03-22-06, 01:42 AM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Here's an example to build a basic dataset (an array of arrays) from the file:

Code:
my @AoA = ();
open(FH,'<file.txt') or die "Can't open file.txt: $!";
while(my $line = <FH>){
   chomp($line);
   push @AoA,[split(/\s+/,$line)];
}
close(FH);
Now you would use @AoA to do whatever it is you need to do.
Reply With Quote
  #4 (permalink)  
Old 03-22-06, 03:13 AM
spunk spunk is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much for the quick reply and although I'm not a Perl coder I fully understand the code except for this:

push @AoA,[split(/\s+/,$line)];

what does the /\s+/ stands for?

And please forgive my ignorance.

Thank you again.
Reply With Quote
  #5 (permalink)  
Old 03-22-06, 12:06 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
/\s+/ means to split the line on one or more spaces, \s being a single space and the + symbol being a quantifier meaning one or more. Tabs (\t) are multiple spaces, so using \s+ is more flexible than using \t to split a file on spaces. But if you are certain the file is tab delimited you could use /\t/ instread of /\s+/.
Reply With Quote
  #6 (permalink)  
Old 03-23-06, 03:00 AM
spunk spunk is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much for the explanation!
Reply With Quote
  #7 (permalink)  
Old 03-24-06, 02:44 AM
spunk spunk is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
It's funny that Perl doesn't have a switch (C or Java) or case (Pascal) function.
Reply With Quote
  #8 (permalink)  
Old 03-24-06, 01:28 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by spunk
It's funny that Perl doesn't have a switch (C or Java) or case (Pascal) function.
I don't know either of those languages, but perl has a few switch modules:

http://search.cpan.org/search?query=switch&mode=all

and at least one case module:

http://search.cpan.org/~rjohnson/Case-0.0.2/Case.pm

But you are right, there is no core switch or case function.
Reply With Quote
  #9 (permalink)  
Old 03-27-06, 01:16 AM
spunk spunk is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
I'm having this small problem. How can I remove one line of my AoA? I'm thinking of using splice, but that removes only one element. Should I use some cycle to do this?

Example:

[1][2][3]
[4][5][6]
[7][8][9]

I want to remove the second row and have something like:

[1][2][3]
[7][8][9]

Thank you again in advance.
Reply With Quote
  #10 (permalink)  
Old 03-27-06, 01:38 AM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Your example is a little confusing, maybe you meant this:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

if you have the above in @AoA to remove the second row:

splice @AoA,1,1;

now @AoA should be:

[1, 2, 3]
[7, 8, 9]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error message not getting displayed. sanjeet Windows .NET Programming 0 12-20-05 10:48 AM
File Handling - Need advice ultiks Job Offers & Assistance 4 12-19-05 12:44 PM
PHP File Handling Alfarin PHP 1 12-15-05 08:00 PM
Need help with File Handling gevorgkhc PHP 3 04-09-05 05:20 AM
File Handling question!! v1brazy PHP 0 09-16-04 10:27 AM


All times are GMT -5. The time now is 04:09 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.