CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Dhamma] Reading Huge .csv and searching it: Edit Log



7stud
Enthusiast

Jan 30, 2013, 5:23 PM


Views: 891
Re: [Dhamma] Reading Huge .csv and searching it

1) Globbing returns a list. Try running this code:

Code
use strict; 
use warnings;
use 5.012;

my @arr = ('hello', 'world');

while (@arr) {
say 'x';
}

What conclusions can you draw from the output? Next try outputting $_ instead of 'x'.

2) Don't use <> to do your globbing; use the glob() function instead. Using <> to do your globbing can introduce a sneaky error in your code: Hard coding values in your code is bad, so you should assign values to variables, and then use the variables, but look what happens here:

Code
use strict; 
use warnings;
use 5.012;

my $pattern = '*.csv';

for my $fname (<$pattern>) {
say;
}

--output:--
readline() on unopened filehandle at 2.pl line 7.


3) You say your csv files are huge, but you are doing this:

Code
@input = <FILE>;


That reads the whole file into memory at one time. Is there a reason you can't read line by line? Too slow?

Code
use strict; 
use warnings;
use 5.012;


my $fname = 'data.txt';

open my $INFILE, "<", $fname
or die "Couldn't open $fname: $!";

while (my $line = <$INFILE>) {
#process line
}



4) You should also be using the 3-arg form of open().

5) You should not use bareword filehandles e.g. FILE.

6) You should declare your variables with my().

7) You should always have these lines at the top of your code:

Code
 
use strict;
use warnings;
use 5.012; #depending on your perl version


8)

Code
foreach my $nr (0..$#input){  
$input[$nr]=~ tr/,/./;}


'for' can be used instead of 'foreach' anywhere in perl, and it's shorter to type. And your loop is better written like this:

Code
my @lines = ('a,a', 'b,b'); 

for my $line (@lines) {
$line =~ s/,/./g;
}

say for @lines;


--output:--
a.a
b.b

$line becomes an alias for each of the elements in the array, so changing $line changes the array. When an experienced perl programmer reads this loop control:

Code
 (0..$#Woerter)


it feels like getting stuck in the eye with a sharp stick. You will rarely use $#arr_name.



8) You have thousands of problems in the code you posted. You need to learn *modern* perl, and it would behoove you to stop reading whatever tutorials you are reading now and buy a begining perl book that was published in the last 5 years.


(This post was edited by 7stud on Jan 30, 2013, 6:24 PM)


Edit Log:
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:23 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:30 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:33 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:41 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:46 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:50 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:55 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:55 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 5:56 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 6:15 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 6:16 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 6:23 PM
Post edited by 7stud (Enthusiast) on Jan 30, 2013, 6:24 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives