On Nov 16, 10:26 am, [EMAIL PROTECTED] (Travis Hervey) wrote: > I have the below code that is reading in a csv file and writing an xml > file. The code as is will work fine if I remove the use strict line. ( > I would prefer not to do this.) However as it is now with the use > strict it gives me a runtime error of: "Can't use string ("") as a HASH > ref while "strict refs" in use at C:\Code\EventXML\TestEventXML.pl line > 15, <EVENTCSV> line 1." Line 15 is where it tries to assign a value to > an array of hashes. ($events[$cnt]{title} = $event_line[0];) This > method of assignment matches what is found > athttp://perldoc.perl.org/perldsc.html#Declaration-of-an-ARRAY-OF-HASHES > under the secont of add a key/value to an element. Any enlightenment > would be appreciated, thanks. > > use strict; > use warnings; > my $filepath = 'C:\Code\EventXML\events.csv'; > my $xmlpath = 'C:\Code\EventXML\events.xml'; > > my @event_line = ""; > my @events = "";
These lines set @event_line and @events to be arrays that contain one element each - the empty string. That is not what you meant to do. What you meant to do was set @event_line and @events to be empty arrays: my @event_line = (); my @events = (); But that is not necessary in Perl. There is no ambiguity when you create a new variable, as there is in some other languages. There is no chance of "junk" values being populated. Just declare the array. It will be an empty array automatically: my @event_line; my @events; > my $line = ""; Same thing here. Just let $line be undef when it starts. There is no need to explicitly set it to the empty string. my $line; > my $cnt = 0; > open(EVENTCSV,$filepath); You are using global bareword filehandles. You are not using the three argument form of open. You are not checking open() for failure. These are all bad. open my $EVENTCSV, '<', $filepath or die "could not open '$filepath': $!"; > while($line = <EVENTCSV>) This is where $line *should* have been declared. while (my $line = <$EVENTCSV>) { > @event_line = split(/,/,$line); This is where @event_line should have been declared. my @event_line = split /,/, $line; > > $events[$cnt]{title} = $event_line[0]; > $events[$cnt]{start} = $event_line[1]; > $events[$cnt]{end} = $event_line[2]; > $events[$cnt]{display} = $event_line[3]; > $events[$cnt]{link} = $event_line[4]; > $cnt++; This should all be replaced creating a single hashref and pushing it onto @events: my $hashref; @{$hashref}{qw/title start end display link/} = @event_line; push @events, $hashref; Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/