
7stud
Enthusiast
Jan 30, 2013, 5:23 PM
Post #6 of 10
(348 views)
|
|
Re: [Dhamma] Reading Huge .csv and searching it
[In reply to]
|
Can't Post
|
|
1) Globbing returns a list. Try running this 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:
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: That reads the whole file into memory at one time. Is there a reason you can't read line by line? Too slow?
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: use strict; use warnings; use 5.012; #depending on your perl version 8)
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:
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: 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)
|