Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem calculating on nils 1

Status
Not open for further replies.

klkot

Programmer
Joined
Jun 6, 2007
Messages
18
Location
US
Using a function such as the one below causes the issue of assuming that if all the intdaymiss i's were nil (missing), it manages to calculate the sum to be 0 rather than nil. Do you know what to do to make sure it sums all nils to stay nil?

$megahash{$patientid[$i]}{$filename}[$dmindx] += $intdaymiss[$i];
 
Your term "nil" is a unique specification of your algorithm. If you want some of your data to stay in that format, then you're going to have to add logic to enforce it. Simply add a test to see if the value is currently "nil" and do whatever you want upon that being true.

- Miller
 
Hi again Miller
Yeah I fixed it with conditionals. They set up the code so that when you sum things, even if you're summing all nils, it sums 0. I'm realizing that is just another sort of frustrating thing that Perl does that you have to go over with with conditionals to fix :(
 
And by the way

THANKS AGAIN MILLER! ;)
 
Well, I'm not sure if it's all that frustrating honestly. You've come up with a unique way of dealing with your data, you shouldn't really expect Perl to know what you mean unless you tell it. When dealing with scalars, Perl has a relatively limited set of things that it aims to do. Basically it will try to "Do What You Mean". So if you're are doing arithmetic operations, it will try to interpret your variable as a number even if it is current a string holder "nil".

One way around this issue would be to make your data into a class and then overload the mathematical operaters. Another way would be to create a class that you can tie to those scalars. But those are just ways of extrapolating the logic into a single location to simplify things. You still need to create the logic though.

- Miller
 
Okay thanks Miller!
You know your stuff. Now I'm going to add another challenging question to the Perl forum.....

 
klkot

From your terminology (observations, datasets) , and the allusions to missing and nil values in the code, I'm guessing that you used to use SAS.

Perl is the Swiss Army Chainsaw of scripting languages, but it's not a specialised statistical processing engine. So don't be surprised that you have code around some of the things you'd normally take for granted in a DATA step...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
klkot,
I suspect that your term nil stands for what in Perl is called undef.
In that case just do the following:
$megahash{$patientid[$i]}{$filename}[$dmindx]+=$intdaymiss[$i]if$intdaymiss[$i];

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Prex1
This is extremely valuable advice that I comprehend and for that I'm giving you a star for perlmeister of the week! ;0
Cheers,
klkot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top