| Author |
Message |
< Erlang ~ Noob Ignorance again! case patterns... |
| seancharles |
Posted: Mon Aug 13, 2007 1:10 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
God, I can't beleive I am posting this but I have just 'thought' myself into a corner and I can't see a way out!
Here goes, anti-embarrassment cream having been rigorously applied to all affected parts...
I have a string, I split into into parts by the "/" character and the first token is ASSUMED to be an integer albeit in string form.
I then want to safely convert this into either the corresponding number or default to number 1, ie clamp the thing if garbage is presented.
Code:
get_page_from_request( A )->
PageId =
case catch list_to_integer( string:tokens( A#arg.appmoddata, "/" )) of
{'EXIT', _} -> 1;
{Number} -> Number
end,
io:fwrite("~B => ~p~n",[Number, A#arg.appmoddata]),
get_page_by_id( PageId ).
When I compile I am told that 'Number is unsafe in case'... HELPPPPP!
I am beginning to realise that pattern matching is a most fantastic thing in Erlang apart from everything else
So, what have I done wrong ?
What have I misunderstood about case ?
What have I misunderstood about patterns ?
I know that 'true ->' can be used to catch-all in if- statements and I have seen the idom '_Else -> ' in case blocks, but I don't yet fully understand '_Else', it doesn't appear to be a reserved word, more it is a catch-all pattern to prevent a case-clause exception.
So, in my case above, how do I get the actual result from calling list_to_integer without the warning message ?
So, can somebody enlighten me please!
 |
|
|
| Back to top |
|
| Mazen |
Posted: Mon Aug 13, 2007 1:17 pm |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
Simply put:
if list_to_integer() crash, you go into the {'EXIT'...} clause and Number never gets bound. Which means that the printout will fail because Number is unbound.
Solution, Either assign the return value of the case clause to Number, OR assign Number to 1 in the exit clause.
Either way Number has to be assigned if your going to use it  |
|
|
| Back to top |
|
| Mazen |
Posted: Mon Aug 13, 2007 1:20 pm |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
Also... I think you will have a problem here:
because I don't think that list_to_integer() can return a tuple  |
|
|
| Back to top |
|
| seancharles |
Posted: Mon Aug 13, 2007 1:22 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
I thought I *was* trying to assign it into PageId, that is where the result gets written to isn't it?
Now you have really confused me!
{Number} was an ERROR!-- should be as shown belowm excuse me for that, I was trying to code my out without thinking!
Code:
get_page_from_request( A )->
PageId =
case catch list_to_integer( string:tokens( A#arg.appmoddata, "/" )) of
{'EXIT', _} -> 1;
Number -> Number
end,
io:fwrite("~B => ~p~n",[Number, A#arg.appmoddata]),
get_page_by_id( PageId ).
So, assuming it IS a string like "4", then *how do I get '4' into PageId*
AGGH!  |
|
|
| Back to top |
|
| Mazen |
Posted: Mon Aug 13, 2007 1:28 pm |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
BAH! Wrong indentation blinds me hehe
Anyway... YES you are assigning PageId to whatever value is returned by the case clause BUT you are still using Number outside the case clause which means that Number could potentially be unbound (thus unsafe)
instead of:
Code: io:fwrite("~B => ~p~n",[Number, A#arg.appmoddata]),
try
Code: io:fwrite("~B => ~p~n",[PageId, A#arg.appmoddata]), |
|
|
| Back to top |
|
| seancharles |
Posted: Mon Aug 13, 2007 1:34 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
GGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH!
Why does it always take somebody else to
point out the <expletive-deleted> obvious mistake in your own code ?
The embarrassment cream was not enough to prevent the heat from the blood-rush to my face from melting the case of my iMac into a twisted pile of plastic.
So, I DO UNDERSTAND patterns more than I thought but
I AM JUST A DUMBASS at reading my own code.
On reflection, I got depressed and saw the error message but NOT THE LINE NUMBER... if I had spotted that then I would have realised it was that obvious.
This is one (another) lesson learned the hard way!
Many thanks for your time,
Sean Charles |
|
|
| Back to top |
|
| Mazen |
Posted: Mon Aug 13, 2007 1:43 pm |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
You have no idea how many times I've asked for other peoples eyes and they walk up to my screen, spend 1 second looking at it, point their finger... and all I want to do next is to blow something up because I have been so frustrated the past 15 minutes going "Why the Hell doesn't it just woooooorrk!!"
Needless to say, I enjoy being the one pointing on the screen instead..   |
|
|
| Back to top |
|
| seancharles |
Posted: Mon Aug 13, 2007 1:46 pm |
|
|
|
User
Joined: 18 Jul 2007
Posts: 57
|
Thanks again Mazen!
I am still killing myself here at my own dumbness! I have been a software engineer for nearly 22 years now and it never ceases to amaze me how gullible and fallible I still am!
I guess thinking the opposite is a 'bad' sign!
Thanks dude! |
|
|
| Back to top |
|
|
|