Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

uninitialized string variable

by jjw92 (Novice)
on Aug 13, 2010 at 16:16 UTC ( #854942=perlquestion: print w/ replies, xml ) Need Help??
jjw92 has asked for the wisdom of the Perl Monks concerning the following question:

I am pulling data out of a .csv file and doing a calculation, then putting the data into an excel file. I tried the following:

if(@$data[$i]->{"Date1"} eq "" || @$data[$i]->{"Date2"} eq ""){ ### getting error from above line $days = "N/A"; } else{ my $t1 = Time::Piece->strptime(@$data[$i]->{"Date1"},"%m/%d/%Y"); my $t2 = Time::Piece->strptime(@$data[$i]->{"Date2"},"%m/%d/%Y"); $days = $t2 - $t1; $days = int($days->days); } $ws->write(1, $i+1, $i+1, $colhdr);

I am getting an error saying that it is an un-initialized variable in the string where marked. The program still runs and gives me the file I want, but I just want to understand and get rid of the error. Thanks for the input
FYI, I am debugging in Komodo IDE



Thanks everyone for input. I found the error. It was in my .csv file. There were a few blank lines so they didn't get the comma separation. After deleting the lines, the error went away and the program works. Thanks again though :)

Comment on uninitialized string variable
Download Code
Re: uninitialized string variable
by JediWizard (Deacon) on Aug 13, 2010 at 16:30 UTC

    You must be running with warnings enabled, which is a good thing, especially if you're reasonably new to Perl. The warning means that a variable that has no value is being used in string context. If you are using Text::CSV to parse the CSV file, it is probably using undef for blank fields. I'd have to look, but I believe there is a parameter to request that Text::CSV uses an empty string $var = ''; instead of $var = undef; for empty columns, which should prevent the warning.

    If that doesn't suit you're liking: you can use something like this: $var ||= ''; to replace the undefined value with a blank string, which would also prevent the warning.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Re: uninitialized string variable
by jethro (Monsignor) on Aug 13, 2010 at 16:33 UTC

    @$data[$i] is the same as @{$data}[$i], maybe that's what you wanted, maybe you wanted @{$data[$i]} instead

      I'm not actually sure why you need the @ at all. I believe that what you probably want is $data[$i]->{"whatever"}. It looks to me as though the poster thinks that the @ is needed because @data is and array (native php coder?). The use of ->{"something"} tells me that $data[$i] contains a hashref, not an array ref. Even if $data[$i] was an array ref, you'd only need the @ to either a. dereference the array, or b. use an array slice.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

        Yes, I am reasonably new to Perl, I am using Text::CSV::Slurp. The @ is used because $data is returned as a reference to an array of hashes, so:

        @$data[0] -> {"data"}

        Returns the first hash inside data, and points to the value associated with the key "data". *unless my understanding is wrong*

Re: uninitialized string variable
by sundialsvc4 (Monsignor) on Aug 13, 2010 at 22:43 UTC

    Unfortunately, you usually can’t guarantee the quality of the input data that you are being asked to process.   Furthermore, it might or might not be “okay” to “just skip” the bad ones.

    Ex minimis, you will need to trap the exceptions that may occur, and attempt to recover gracefully.   More likely, you will have to process the input files twice... first to verify data integrity, then to process it.

      At the very least, the first step to debugging would be for the OP to print out (delimited) what was read, what the contents of $data looked like, etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://854942]
Approved by kennethk
Front-paged by jonadab
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-07-11 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls