Author Topic: Assigning values in udt to member with array?  (Read 2447 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Assigning values in udt to member with array?
« on: February 02, 2015, 05:52:21 AM »
Hi Charles,

I would like to assign values to a structure which holds a member with an array, something like this:

type note
    int iDur
    int iNote[2]
end type

note noteseq[] = {...}

but it seems that I cannot do this directly. The following approach would work for me, but maybe there is a simpler and more general way to do this? I would like to prevent wasting memory by using a second array of user defined type.

Roland

Code: [Select]
' Assigning values in udt to member with array

includepath "$/inc/"
#include "Console.inc"

type a_note
    int iDur
    int iNote1, iNote2
end type
   
a_note notes[] ={ 
    {110, 69, 81}, {110, 67, 79}, {990, 69, 81}, {220, -1, -1}, {110, 67, 79},
    {110, 65, 77}, {110, 64, 76}, {110, 62, 74}, {220, 61, 73}, {440, 62, 74},
    {1980,-1, -1}, {110, 57, 69}, {110, 55, 67}, {990, 57, 69}, {220, -1, -1},
    {220, 52, 64}, {220, 53, 65}, {220, 49, 61}, {440, 50, 62}, {1980, -1, -1}
    }
   
print "spanof(notes) = " spanof(notes) cr

for x =1 to spanof(notes)
    print notes[x].iDur " " notes[x].iNote1" " notes[x].iNote2 cr
next

'------------------------

type note
    int iDur
    int iNote[2]
end type

note noteseq[spanof(notes)]

print cr "spanof(noteseq) = " spanof(noteseq) cr

for x =1 to spanof(noteseq)
    noteseq[x].iDur = notes[x].iDur
    noteseq[x].iNote[1] = notes[x].iNote1
    noteseq[x].iNote[2] = notes[x].iNote2
next   

for x =1 to spanof(noteseq)
    print noteseq[x].iDur " " noteseq[x].iNote[1] " " noteseq[x].iNote[2] cr
next

print cr "Enter ,,,"
waitkey


Charles Pegge

  • Guest
Re: Assigning values in udt to member with array?
« Reply #1 on: February 02, 2015, 01:10:36 PM »
Hi Roland,

That is the best way to do it.

In any case, using a member array of inotes inside the type would not save space, instead of using individual inotes. How may notes do you need inside the type?

Aurel

  • Guest
Re: Assigning values in udt to member with array?
« Reply #2 on: February 02, 2015, 02:23:12 PM »
well
i just receive error about stray comma,sines when o2 support such a array-s?
this array looks to me like DATA block  :o

Arnold

  • Guest
Re: Assigning values in udt to member with array?
« Reply #3 on: February 02, 2015, 04:32:13 PM »
Hi Charles,

the structure was taken from an example in "Programming Windows", Chapter 22, Bachtocc.c The link is:

ftp://ftp.charlespetzold.com/ProgWin5/Chap22/BachTocc/BachTocc.c

To convert the example to OxygenBasic I used an array with one dimension

   int iDur = 1
   int iNote = 2
   
   int noteseq[] =
                    {...} 

and adapted the statements to fit to the Bachtocc program. I was unsure if I could use a similar construct as in the Bachtocc example, perhaps applying a macro or pointer manipulation.

Roland

Charles Pegge

  • Guest
Re: Assigning values in udt to member with array?
« Reply #4 on: February 02, 2015, 10:05:21 PM »
Hi Roland,

Another strategy is to create a mini-scripting language. It will give you all the flexibility you will need without using fixed structures. Here is an example, using the same note data:

Code: OxygenBasic
  1. includepath "$/inc/"
  2. include "console.inc"
  3. include "parseutil.inc"
  4.  
  5. ! main() as int
  6.  
  7. main
  8. waitkey
  9.  
  10.  
  11. function main() as int
  12.  
  13. string notes="
  14.   # n duration and notes
  15.   n 110 69 81
  16.   n 110 67 79
  17.   n 990 69 81
  18.   n 220 -1 -1
  19.   n 110 67 79
  20.   n 110 65 77
  21.   n 110 64 76
  22.   n 110 62 74
  23.   n 220 61 73
  24.   n 440 62 74
  25.   n 1980 -1 -1
  26.   n 110 57 69
  27.   n 110 55 67
  28.   n 990 57 69
  29.   n 220 -1 -1
  30.   n 220 52 64
  31.   n 220 53 65
  32.   n 220 49 61
  33.   n 440 50 62
  34.   n 1980 -1 -1
  35. "
  36. sys i=1
  37. sys le=len(notes)
  38. string wr
  39. sys ct
  40. do
  41.   wr=getword notes,i
  42.   select asc wr
  43.   case 0   : exit do
  44.   case "#"  : endline notes,i 'ignore comment
  45.  case "n" : ct++ printl "n" ct
  46.   case "-","0" to "9" : print tab wr
  47.   end select
  48. end do
  49. printl
  50. end function
  51.  

Arnold

  • Guest
Re: Assigning values in udt to member with array?
« Reply #5 on: February 04, 2015, 03:05:20 AM »
Hi Charles,

this file parseutil.inc seems to be a really very powerful tool. I will need some time to explore it's capabilities.

Roland