1. Problem with mydata.ex
- Posted by davidmosley Feb 21, 2010
- 1103 views
Hi I have a problem with mydata.ex,When i changed the fields and then i ran the program,i add a record,then i tried to list all records i get a error in the show procedure see below: I can not firure out why,can you help thanks David Mosley davidpmosley@gmail.com
error file:
C:\euphoria\DEMO\mydata.ex:86 in procedure show() subscript value 4 is out of bounds, reading from a sequence of length 3 f = 1 key = {53'5',48'0',53'5',51'3',50'2',53'5',53'5',53'5',56'8',53'5'} data = { {77'M',111'o',115's',108'l',101'e',121'y'}, {68'D',97'a',118'v',105'i',100'd'}, {80'P'} } i = 5 ... called from C:\euphoria\DEMO\mydata.ex:162 in procedure list() f = 1 rec = 2 ... called from C:\euphoria\DEMO\mydata.ex:205 in procedure main() command = {76'L'} printer = <no value> ... called from C:\euphoria\DEMO\mydata.ex:222 Global & Local Variables c:\euphoria\include\machine.e: mem = 46013088 check_calls = 1 c:\euphoria\include\misc.e: pretty_end_col = <no value> pretty_chars = <no value> pretty_start_col = <no value> pretty_level = <no value> pretty_file = <no value> pretty_ascii = <no value> pretty_indent = <no value> pretty_ascii_min = <no value> pretty_ascii_max = <no value> pretty_line_count = <no value> pretty_line_max = <no value> pretty_dots = <no value> pretty_fp_format = <no value> pretty_int_format = <no value> pretty_line = <no value> c:\euphoria\include\file.e: SLASH = 92'\' my_dir = -2 c:\euphoria\include\get.e: input_file = <no value> input_string = <no value> string_next = <no value> ch = <no value> c:\euphoria\include\database.e: current_db = 3 current_table = 23 db_names = { {109'm',121'y',100'd',97'a',116't',97'a',46'.',101'e',100'd', 98'b'} } db_file_nums = {3} db_lock_methods = {0} current_lock = 0 SLASH_CHAR = {92'\',47'/'} key_pointers = {620,481} db_fatal_id = 0 mem0 = 46013104 mem1 = 46013105 mem2 = 46013106 mem3 = 46013107 memseq = {46013104,4}
Mydata.ex
----------------------- Simple Customizable Database ----------------------- -- This program uses the Euphoria Database System (EDS) to create and -- maintain a simple database. You can customize this program by modifying -- the FIELDS variable (below). You'll have to delete or rename the -- "mydata.edb" database file if you change the number of fields. constant FIELDS = { -- The first field is the "key" value used for lookups, -- and it must only occur in one record. We used the phone number -- (like our local dry cleaner does). -- You might want to use the surname, or invent a "person number" -- as an artificial but unique key. "Phone number", -- The other fields can be whatever you like. Feel free to change -- or add more fields... "Last Name", "First name", "Middle Initial", "Address", "City", "State", "Zip", "Email", "Web Site" } -- file to store the database in: constant MYNAME = "mydata.edb" include database.e -- Euphoria Database System include get.e include sort.e include wildcard.e constant KEYBOARD = 0, SCREEN = 1, ERROR = 2 constant TRUE = 1 constant WHITE_SPACE = " \t\n" constant FORM_FEED = 12 type file_number(integer x) -- document which vars are used as file numbers return x >= 0 end type procedure myfatal(sequence msg) -- fatal error puts(ERROR, '\n' & "An unexpected error occurred: " & msg & '\n') ? 1/0 -- too see call stack end procedure function user_input() -- get user input from keyboard object line while TRUE do line = gets(KEYBOARD) if sequence(line) then -- delete any leading whitespace while find(line[1], WHITE_SPACE) do line = line[2..length(line)] if length(line) = 0 then exit end if end while if length(line) > 0 then exit end if end if puts(SCREEN, "\n? ") end while -- delete trailing whitespace while find(line[length(line)], WHITE_SPACE) do line = line[1..length(line)-1] end while return line end function procedure show(file_number f, object key, object data) puts(f, "\n" & key & '\n') -- changed for i = 2 to 1 for i = 2 to length(FIELDS) do puts(f, '\t' & data[i-1] & '\n') end for end procedure procedure add() -- add a new record to the database sequence key, data integer f puts(SCREEN, "\n\t" & FIELDS[1] & ": ") key = user_input() f = db_find_key(key) if f >= 1 then show(SCREEN, db_record_key(f), db_record_data(f)) puts(SCREEN, "Do you want to update this record? (y/n) ") if find('n', gets(0)) then return end if end if data = {} for i = 2 to length(FIELDS) do puts(SCREEN, "\n\t" & FIELDS[i] & ": ") data = append(data, user_input()) end for puts(SCREEN, '\n') if f >= 1 then -- update data part of record db_replace_data(f, data) else -- insert new record if db_insert(key, data) != DB_OK then myfatal("insert failed!\n") end if end if end procedure procedure delete() -- delete a record, given first field sequence name integer d puts(SCREEN, "\n\t" & FIELDS[1] & ": ") name = user_input() d = db_find_key(name) if d < 0 then puts(SCREEN, "\n\tnot found\n") return end if show(SCREEN, db_record_key(d), db_record_data(d)) puts(SCREEN, "Delete? (y/n) ") if find('n', gets(0)) then return end if db_delete_record(d) end procedure procedure find_name() -- find the record that matches the surname sequence name integer f puts(SCREEN, "\n\t" & FIELDS[1] & ": ") name = user_input() f = db_find_key(name) if f < 0 then puts(SCREEN, "\n\tnot found\n") return end if show(SCREEN, db_record_key(f), db_record_data(f)) end procedure procedure list(file_number f) -- list the entire database to a device puts(f, '\n') for rec = 1 to db_table_size() do -- changed f to screen show(f, db_record_key(rec), db_record_data(rec)) end for end procedure procedure main() sequence command file_number printer -- open or create the database if db_open(MYNAME, DB_LOCK_NO) != DB_OK then if db_create(MYNAME, DB_LOCK_NO) != DB_OK then myfatal("Couldn't create database") end if if db_create_table("phone numbers") != DB_OK then myfatal("couldn't create table") end if end if -- select the (only) table in the database if db_select_table("phone numbers") != DB_OK then myfatal("couldn't select table\n") end if -- prompt the user for his command clear_screen() puts(SCREEN, "\t\tSimple Database\n") while TRUE do puts(SCREEN, "\n(a)dd, (d)elete, (f)ind, (l)ist, (p)rint, (q)uit: ") command = upper(user_input()) if 'A' = command[1] then add() elsif 'D' = command[1] then delete() elsif 'F' = command[1] then find_name() elsif 'Q' = command[1] then exit elsif 'L' = command[1] then list(SCREEN) elsif 'P' = command[1] then printer = open("PRN", "w") if printer = -1 then puts(SCREEN, "Can't open printer device\n") else list(printer) puts(printer, FORM_FEED) close(printer) end if else puts(SCREEN, "\nsay what?\n") end if end while end procedure main()
[reformatted with console and eucode tags..matt]
2. Re: Problem with mydata.ex
- Posted by mattlewis (admin) Feb 21, 2010
- 1073 views
Hi I have a problem with mydata.ex,When i changed the fields and then i ran the program,i add a record,then i tried to list all records i get a error in the show procedure see below: I can not firure out why,can you help thanks
The error file is telling you what happened. Looking at the relevant pieces:
error file:
C:\euphoria\DEMO\mydata.ex:86 in procedure show() subscript value 4 is out of bounds, reading from a sequence of length 3 f = 1 key = {53'5',48'0',53'5',51'3',50'2',53'5',53'5',53'5',56'8',53'5'} data = { {77'M',111'o',115's',108'l',101'e',121'y'}, {68'D',97'a',118'v',105'i',100'd'}, {80'P'} } i = 5
procedure show(file_number f, object key, object data) puts(f, "\n" & key & '\n') -- changed for i = 2 to 1 for i = 2 to length(FIELDS) do puts(f, '\t' & data[i-1] & '\n') end for end procedure
So, the problem is that the sequence data has 3 elements, but you were trying to access the 4th. Somehow, you got a record with too few fields. Did you perhaps run this before where it was saving fewer fields? So it might have loaded in what is now an incomplete record? You might try deleting the mydata.edb file and then start over to make sure that your data is homogeneous.
Matt
3. Re: Problem with mydata.ex
- Posted by ChrisB (moderator) Feb 21, 2010
- 1067 views
- Last edited Feb 22, 2010
Hi
In line 86 the for loop is counting through the number of FIELDS, which has already been defined at the top of the program.
The number of items in the data variable is less than the number of items in the FIELDS variable.
The loop continues counting the FIELDS items, but exceeds the number of data items, hence throwingthe variable.
Cursorally (?) the correct way would be
for i = 2 to length(data) do puts(f, '\t' & data[i-1] & '\n') end for
However, this is only a very cursory look, and this may still not fit your requirements.
Chris
4. Re: Problem with mydata.ex
- Posted by davidmosley Feb 22, 2010
- 1078 views
Hi guys I just deleted the database and started over agian and it works now silly me did not think about that lol,lol Thank for the help David Mosley
5. Re: Problem with mydata.ex
- Posted by ChrisB (moderator) Feb 23, 2010
- 1008 views
Hi
And it will continue to do so until length(data) > length(FIELDS)
Chris
6. Re: Problem with mydata.ex
- Posted by mattlewis (admin) Feb 23, 2010
- 991 views
And it will continue to do so until length(data) > length(FIELDS)
Yes, but that problem is solved, at least until he changes the format assumed in the code. The problem was trying to use data written with the original code using code changed to assume a different format.
Matt