1. Improved Val() Function

I needed to improve my Val() function because the original was having some major issues. Here is my new copy:
Code:
```def val(data):
i_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\$', '+', '-']
start_char = [data[0]]
end_char = [0]
got_end = [False]
for i in data:
if got_end[0] == True:
break
else:
if i not in i_list:
end_char[0] = i
got_end[0] = True
else:
end_char[0] = data[-1]
got_end[0] = True
if start_char[0] not in i_list:
return 0
else:
if end_char[0] in i_list:
return int(data)
else:
return int(data[data.find(str(start_char[0])):data.find(str(end_char[0]))])```
I would appreciate some comments on this code. Maybe where something could go wrong or a better way of arranging code. I know this can't be as good as it can get.
2. Hi,
I think you lost your way a bit with all the unnecessary list elements.
end_char = [0]
got_end = [False]
are very redundant [] !!!

You also don't handle the case where none of the data matches your format.

The code allows any combination of digits +,- and \$ in any quantity. If you want to check the format as well as get the number then regular expressions are probably the best way to go - just to prevent the code from getting too complicated to understand

Here is my spin on the same function using regular expressions (I made a few assumptions about the format you wanted) ...
Code:
```[001] import re
[002] ldollar = re.compile("([\+-]?)(\\$?)(\d+)") # pre-compile the regular
[003]                                             # expression for speed
[004]
[005]
[006] def DollarVal(data=''):
[007]     """ DollarVal(data)
[008]     Search data string and convert
[009]     the first sub-string matching the format:
[010]     (+,-)(\$)123456
[011]     to a long.
[013]     """
[014]     sub = ldollar.search(data)
[015]     if sub:
[016]         elements = sub.groups()
[017]         ans = long(elements[0]+elements[2])
[018]     else:
[019]         ans = None
[020]     return ans
[021]
[022] print DollarVal("-\$1267")
[023] print DollarVal("-1267")
[024] print DollarVal("+\$1267")
[025] print DollarVal("1267")```
The regular expression in line 2 can be tweaked to make the match requirements more relaxed or tighter as required.
Python Regular Expressions

grim
Last edited by Grim Archon; April 1st, 2005 at 03:53 AM. Reason: unnecessary escape removed
3. I thought an explanation might be helpful:
Code:
`ldollar = re.compile("([\+-]?)(\\$?)(\d+)")`
The string passed to the re.compile function is a series of rules to apply when checking a string. ldollar becomes a regular expression object with those rules built in.

"([\+-]?)(\\$?)(\d+)" can be interpreted as this:

Find a sub-string that has:

• 0 or 1 sign characters (no sign or + or - but not both )
• 0 or 1 Dollar symbol
• A series of 1 or more digits (as many as you can match)

The () characters are used to group matching sequences for the ldollar.groups() method

So in my code ldollar.groups() returns a list:
["sign symbol","dollar symbol","the digits"]
if there is no sign or dollar then the first two elemenst can be "".

grim
4. I like the way the code is arranged in your code. The only problem, is I do the following:
Code:
```>>> DollarVal('554ghgf\$')
554L```
That 'L' shouldn't be there. With the way I made my function, it does almost exactly what the VB6 Val() function does. It returns all integers, -'s, +'s, and \$'s up to the first different (not like the others) character that it finds. If the first character is different, it returns 0. Thanks still.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2004
Posts
84
Rep Power
11
Just checking - you know that the L is just there to indicate that the value is of the long data type, and doesn't actually represent a character in the value returned by the function? if you wrote

>>> print DollarVal('554ghgf\$')

you would just get 554 without the trailing L.
6. I see. Thank you for pointing that out. But why does it only add the L if you are not using print? I don't see why it should make a difference, shouldn't it be returning the same data?
7. Originally Posted by †Yegg†
I see. Thank you for pointing that out. But why does it only add the L if you are not using print? I don't see why it should make a difference, shouldn't it be returning the same data?
When you are executing the function from the listener, it automatically displays the last returned value as a Python literal. This is not a part of the program; it is a behavior of the listener itself. When you use print, however, it actually is the program performing the built-in printing function, which displays the results as text; since numbers don't normally have type indicators in the real world , it does nto showe the 'L'. If you were to run the Python program from the command line (or by double-clicking on the program file's icon, in Windows at least will have the same effect, providing the .py file type is registered), you would only see the text displayed by the print function.
Last edited by Schol-R-LEA; April 2nd, 2005 at 02:15 PM.
8. Ok, thank you for clearing that up.