1
2
3
4
5
6
7
8
9
10
11
12
use warnings;
use strict;
use List::Util qw(min);
print "Script open!\n";
### Initialize Input
my $input_file_name = "test.csv";
open (my $fil ,"<",$input_file_name) or die "Can not open file $input_file_name!";
my $size_of_array_chunks = 10; ### set the number in wich chunks you want to split the array. Necessary because otherwise the generated statement in db_query would be too long
my @allrows = <$fil>; ### stores all rows of the csv file
1
2
3
4
5
6
7
foreach my $set (0..int(@allrows-1/$size_of_array_chunks)) ### generate the chunks (Explanation: with "$size_of_array_chunks = 100" this loop will do "@allrows[0..99]", then "@allrows[100..144]" if the File has 155 Rows)
{
my $testval = (@allrows[$size_of_array_chunks*$set..min($size_of_array_chunks*($set+1)-1,$#allrows)]);
print "$testval\n";
do_sth(@allrows[$size_of_array_chunks*$set..min($size_of_array_chunks*($set+1)-1,$#allrows)]);
#if the file has 17 lines, the code still runs 15 times even though the file is already at the end
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sub do_sth
{
my $db_query_counter = 0; ### necessary to differenciate between the first part of the sql query and every following OR
my @missing_query_part = ();
foreach my $string (@_)
{
# print "\n";
# print "(foreach) sub input: ${string}";
chomp(${string});
${string}=~ s/\"//g; # Removes all
my @values = split(/\s*,\s*/, ${string});
push our @chunk_data_array, [@values]; ## [] referenz
### (.....)
}
@allrows= <$file>
1 2 3 4 5 6 7 8 9 10 11 12 13 14
sub naechstes_paket { my $handle = shift; my @lines = (); while (@lines < 10) { my $line = <$handle>; last unless $line; push @lines, $line } return @lines; } while (my @next_lines = naechstes_paket($fil) { ... mach was }
1 2 3 4 5 6 7 8 9 10 11
my $maximum = 10; my @array = 1 .. 43; # max 10 Elemente auf einmal rausholen; die Elemente werden aus @array entfernt! while ( my @partial = splice( @array, 0, $maximum ) ) { # DB Update oder sonstwas treiben ... printf "fetched %d elements from array.\n", scalar @partial; }
our $globalrowcounter = 0; ### Counts +1 for every run thorugh the "foreach my $string (@_)" loop at the sub do_sth ---> this value is equal to the row number in the file