Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with multidimensional arrays
#1
Hello,

I need to move down two lines in a log file then check the next 10 lines or so to see if they start with "Urgent". If they do i need to take each line and populate an array with each word in the line. I want the array to have the first dimension MyArray[0] [0] to be the first word (Urgent) in the line and the next, MyArray[0] [1] to be the second word, MyArray[1] [0] first word next line. Each line has 6 parts to it delimited by a space....nothing i've tried has worked, could i ask you for an example.

I also need to know how to access and modify each dimension. A small example would be fine. I understand how to work with arrays in other languages but nothing i've tried in QM has worked. I've read everything i can find on Arrays in QM help and on the forum and I'm stuck.

My log files are one file per day with multiple appends during each day. So after I go through the first chunk of text i need to move down to the next appended chunk of text in the file and start again. Three blank lines delimit each chunk of text.

Can you help?

If you think using TYPE or CLASS would be a better solution by all means suggest away. I know thats bascally what i'm doing is creating a CLASS but I have no idea how to do that either.

Thanks in advance for any help you can give on this.
Bob
#2
This example uses type.
Code:
Copy      Help
;str logfiledata.getfile(...)
str logfiledata=
;header
;
;not urgent a b c d e
;urgent a b c d e
;urgent a b c d e
;not urgent a b c d e
;urgent a b c d e
;
;
;
;not urgent a b c d e
;urgent a b c d e
;urgent a b c d e
;not urgent a b c d e
;urgent a b c d e
;


type X765 str'u str'a str'b str'c str'd str'e ;;declare type to hold 6 words
ARRAY(X765) a
str s
foreach s logfiledata
,if(!s.len) continue
,if(!s.begi("urgent")) continue
,X765& e=a[a.redim(-1)] ;;add 1 new element and get reference to it
,if(tok(s &e.u 6 " ")!=6) end "bad format"
,out "%s, %s, %s, %s, %s, %s" e.u e.a e.b e.c e.d e.e
,

This example uses 2-dim array.
Code:
Copy      Help
;str logfiledata.getfile(...)
str logfiledata=
;header
;
;not urgent a b c d e
;urgent a b c d e
;urgent a b c d e
;not urgent a b c d e
;urgent a b c d e
;
;
;
;not urgent a b c d e
;urgent a b c d e
;urgent a b c d e
;not urgent a b c d e
;urgent a b c d e
;


ARRAY(str) a.create(6 0) ;;first dimension has 6 elements, one for each word; second dimension is resizable. Imagine that first dimension is horizontal (x), second vertical (y).
str s
foreach s logfiledata
,if(!s.len) continue
,if(!s.begi("urgent")) continue
,int ni=a.redim(-1) ;;add 1 new element (6-string array) in second dimension
,if(tok(s &a[0 ni] 6 " ")!=6) end "bad format"
,out "%s, %s, %s, %s, %s, %s" a[0 ni] a[1 ni] a[2 ni] a[3 ni] a[4 ni] a[5 ni]
,
#3
Thank you Gintaras you really are amazing. I don't know how you do it so damn fast. Both examples work perfectly. I think i'll use the TYPE solution as i can see it will be easer to manipulate the data. I can use descriptive variable names, easier to remember.

One other question though, I need to be able to perform other actions after i read in the first block of text but before i move on to the next block. How can i get it to break to allow me to run other code (which i have worked out already) and then go back and find the next chunk to work on. The chunks are delimited by 3 blank lines. Sorry i wasen't clear on that before.

Thanks again for one of the best works of software art ever and THE BEST support ever!!!

If everyone in Lithuania was like you you'd have taken over the world a LONG time ago! :lol:

Bob
#4
For example, count empty lines.

Code:
Copy      Help
;str logfiledata.getfile(...)
str logfiledata=
;header
;
;not urgent a b c d e
;urgent a b c d e
;urgent a b c d e
;not urgent a b c d e
;urgent a b c d e
;
;
;
;not urgent a b c d e
;urgent a b c d e
;urgent a b c d e
;not urgent a b c d e
;urgent a b c d e
;


type X765 str'u str'a str'b str'c str'd str'e ;;declare type to hold 6 words
ARRAY(X765) a
str s; int i ie ib
logfiledata+"[][][][]" ;;add several empty lines

foreach s logfiledata
,i+1; if(i<3) continue ;;skip first two lines
,if(!s.len) ;;empty line
,,ie+1 ;;count empty lines
,,if(ie=3) ;;end of block
,,,ib+1
,,,out "end of block %i" ib
,,continue
,
,ie=0
,if(!s.begi("urgent")) continue
,X765& e=a[a.redim(-1)] ;;add 1 new element and get reference to it
,if(tok(s &e.u 6 " ")!=6) end "bad format"
,out "%s, %s, %s, %s, %s, %s" e.u e.a e.b e.c e.d e.e
,
#5
Once again thank you so very much for the fast, precise help.

Works like a cham.

Nice example of a foreach loop with multiple counters too. I think I've learned more about QM's from your (and other helpful people on the forum) examples here in the forum than anywhere else. I did learn alot about regular expresions from the Micro$oft tutorial but other than that... :lol:

I see what I was doing wrong with setting up the array. My syntax wasen't right and I didn't realize they set up backwards to what I was thinkng. I also have a much better grasp of how to setup and use a TYPE now.

I'm sure I'll be back with more questions, I want to get to know QM's to the point where it's worth buying the PRO version.

Thank you so very much,
Bob


Forum Jump:


Users browsing this thread: 2 Guest(s)