Reputation: 2216
Hi here is a small snipit of a very large perl program I am working on, however I believe the error is around this part.
my $headd = "test,test1,test2,test3,ect";
my @headers = split(',', $headd);
CONNECT TO DATABASE STUFF THAT WORKS FINE
my %column_mapping = (
"GPS ALT" => 'GPS_ALT',
"GPS DTS" => 'GPS_DTS',
"GPS FIX" => 'GPS_FIX',
"GPS HDG" => 'GPS_HDG'
)
my $sql = sprintf 'INSERT INTO tablename ( %s ) VALUES ( %s )',
join( ',', map { $column_mapping{$_} } @$headers ),
join( ',', ('?') x scalar @$headers ); # note the list around the '?'
Since I am fairly new to perl, if I had to guess I would say its around here
join( ',', map { $column_mapping{$_} } @$headers ),
join( ',', ('?') x scalar @$headers );
I think I have the wrong data types for headers or something, but I am not entirely sure what the problem is, it just crashes around those lines.
If you happen to see something I did wrong that would be great:)
Upvotes: 1
Views: 75
Reputation: 69264
Your main error is in not adding use strict
and use warnings
to the top of your Perl code. These lines will find a number of problems that often sneak into Perl programs. They would have found this problem.
The problem is that you declare an array called @headers
.
my @headers = split(',', $headd);
But the next time you try to use it, you are assuming that it is an array reference called $headers
.
join( ',', map { $column_mapping{$_} } @$headers )
In Perl, $headers
and @headers
are two completely different variables with absolutely no connection between them. As you have never given $headers
a value, trying to dereference it (@$headers
) was never going to go well.
But you've worked that out from the comments. You've made the suggested fix (replace @$headers
with @headers
) and now you get a different error:
the problem is saying I have an initialized value in the join
I assume you mean "uninitialized" :-)
Sounds like the problem is on the same line that we discussed above (now with the fixed syntax):
join( ',', map { $column_mapping{$_} } @headers )
So what's happening here? Well, we are effectively translating values to new values. For each value in @headers
we look it up in %column_mappings
and return the associated value from the hash.
How could that give us an undefined value? Well, what happens if we look up a key in a hash that doesn't actually exist in the hash? Perl gives us the special value "undef". Which would trigger the warning that you are getting.
So my suggestion to you is that you double-check the values that you are getting in @headers
(which will be the values from $headd
) and make sure that all of those possible values have an associated key in %column_mappings
.
One workaround would be to set a default value in the map
. Something like this perhaps:
join( ',', map { $column_mapping{$_} // 'MISSING MAPPING' } @$headers )
That will almost certainly break your database interaction at some point further down the line - but at least it will be obvious what the problem is!
Update: To eliminate the problematic values before they get into @headers
:
my @headers = grep { exists $column_mapping{$_} } split(',', $headd);
You'll want to move this statement after the definition of %column_mappings
(for hopefully obvious reasons).
Upvotes: 2