Time to work on those two subs I promised I would explain, &grab_data and &process_record;
sub grab_data {
# Takes a record, and grabs it into an array
my ($line) = shift;
This, as usual, makes sure a local variable stays local... so it will never leave this sub. Its value is also set to what is sent to the sub procedure,.
my (@data) = split (/\Q$delimeter\E/o, $line);
Splits the line using the delimeter for the fields, and puts each value into the array @data
foreach (@data) {
Now @data
re-iterates over each value
s/``/\n/g; # Change `` back to newlines..
s/~~/$delimeter/g; # get the delimiter back
Since Each Value is referred to as $_
, you can directly write in the regular expression to access $_
, without the use of $wuddever =~ s/wuddever/g;
}
Ends the Foreach Loop
return @data;
Returns the contents of @data
}
End of &grab_data
sub process_record {
# changes the data format to something we can use
my (@array) = @_;
You know the drill... takes data sent to it, refers to it as @array
, which is declared as a my variable
my (%record);
The hash %record
is declared as a my variable
my ($num) = 0;
$num
is also declared as a my variable, and set to a value of 0
. This variable keeps track of where we are in the array when we change it into a hash format.
%record = map { $db_fields[$num] => $array[$num++] } @_;
Map evaluates the block ( { stuff in between here is a block } )
for each element of a list (locally setting $_
to each element). It will return the list of results of each evaluation. In this case, it causes the hash, %record
, to be composed of each element in @db_fields
(remember? It was made near the top of the script), and increments each value of $num
each time... so it accesses a different array element each time.
return %record;
Simply returns the hash %record
}
End of sub process_record
Phew.... time for a break, even if you didn' t understand all that. Just go for a quick walk and come right back so we can hack at this some more.