Donna writes:
"sometimes you just gotta role your own, you know? :-)"
I hear that!! ;-)
Now, question...if I'm reading your scripts correctly,
in your return variable (assuming there is a match)
on the "search" script where you do the following:
setvar !_s_return_var "![_ma_rec!bin_return]"
aren't you just putting a line like the following into
the "_s_return_var"...
"1 one" #(no quotes)
???
I know you can play with things like "str" to pull out
the "one" but I was under the impression you really
wanted to input the key, and get ONLY the value
back ready to go.
I bring this up because I finally got "smart" and did a
"help functions" at the CI. By doing that I rediscovered
the "word" function.
word() will extract one "word" from a string. Word being
defined here as a group of characters that does not contain
a delimiter which, you can supply (or just accept the defaults).
Now as I see it, this has two very good implications...
1. The obvious, you can now return JUST the value you're
looking for.
2. If used correctly within your "mkarray" script, you can
accept data that is NOT ordered, misaligned, and no need
for even a binary search, because you have direct access
once the array is created.
Here's how:
from this ...
=========================
'mkarray'
setvar _ma_eof finfo(HPSTDIN,"eof")
setvar _ma_i 0 # <== get rid
of
while setvar(_ma_eof,_ma_eof-1) >= 0
input _ma_rec
setvar _ma_i _ma_i + 1 # <===get rid of
setvar _ma_rec!_ma_i rtrim(_ma_rec) #<===modify
setvar _ma_key!_ma_i str(_ma_rec,key_begin,key_length) # <===get rid of
endwhile
======================
to this:
=========================
'mkarray'
setvar _ma_eof finfo(HPSTDIN,"eof")
while setvar(_ma_eof,_ma_eof-1) >= 0
input _ma_rec
setvar _ma_key word("!_ma_rec",,1) # <=== is key only [added]
setvar _ma_val!_ma_key word("!_ma_rec",,2) # <=== is value only
endwhile
======================
We use the ACTUAL "key" numeral (the first data item
in the rec) as the array index, (which makes it more like
a hash).
Also, key_begin and key_length are no longer needed,
which if I can get around hard coding where things should
be, I like that better! :-) _ma_i isn't needed in "mkarray"
either. So references to these should be removed from the
search job.
Now to search for a specific value to use IF available,
instead of the "binsrch" routine you have direct access
as follows:
===========================
parm key #where "key" is the exact first item from the input file
if bound(_ma_val!key)
<do something with _ma_val!key>
else
echo No value match for key !key
endif
===========================
Here's a modified "mkarray" with the "binsrch" replacement
code inside it. And the output from running the command.
===========================
parm key
setvar _ma_eof finfo(HPSTDIN,"eof")
while setvar(_ma_eof,_ma_eof-1) >= 0
input _ma_rec
setvar _ma_key word("!_ma_rec",,1)
setvar _ma_val!_ma_key word("!_ma_rec",,2)
endwhile
if bound(_ma_val!key)
showvar _ma_val!key
else
echo No value match for key !key
endif
=============================
datafile =
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
12 twelve
13 thirteen
14 fourteen
15 fifteen
test to find data
LEE(SYS): mkarray 5 < datafile
_MA_VAL5 = five
And a test if no match is found
LEE(SYS): mkarray 29 < datafile
No value match for key 29
Now let's make this REALLY interesting...
datafile =
2 two
11 eleven
3 three
5 five
6 six
7 seven
15 fifteen
100001 abignumber
8 eight
13 thirteen
9 nine
10 ten
12 twelve
39 misaligned_data
1 one
4 four
14 fourteen
Now we have a number that is so large it extends into
the string column space wise. We also have key "39"
whose data is totally misaligned.
LEE(SYS): mkarray 39 < datafile
_MA_VAL39 = misaligned_data
LEE(SYS): mkarray 100001 < datafile
_MA_VAL100001 = abignumber
This defeats (some) limitations on data alignment, and order.
Here is a "showvar _ma@" after running the command
using the mixed up datafile
LEE(SYS): showvar _ma@
_MA_EOF = -1
_MA_REC = 14 fourteen
_MA_KEY = 14
_MA_VAL1 = one
_MA_VAL2 = two
_MA_VAL3 = three
_MA_VAL4 = four
_MA_VAL5 = five
_MA_VAL6 = six
_MA_VAL7 = seven
_MA_VAL8 = eight
_MA_VAL9 = nine
_MA_VAL10 = ten
_MA_VAL11 = eleven
_MA_VAL12 = twelve
_MA_VAL13 = thirteen
_MA_VAL14 = fourteen
_MA_VAL15 = fifteen
_MA_VAL100001 = abignumber
_MA_VAL39 = misaligned_data
HTH
Ron Wuerth
Computer Operations Specialist
Virginia International Terminals
Phone: 757-391-6194
Fax : 757-391-6223
mailto:[log in to unmask]
* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *
|