PDA

View Full Version : position of an item in a QStringList



jaca
28th May 2008, 20:28
Hi!

I have the following:


QStringList list;
//list == ["SPNT", "123", "321", "SPNT", "111", "122", "222", "211", "SPNT", "001", "SPNT"]

I wanted to know the position of each SPNT. I used list.indexOf ( "SPNT") but only returns the first position. How do I know the position of the string SPNT in QStringList?
My idea is to print a file in the numbers between the SPNT this way:
123 321
111 122 222 211
001

jpn
28th May 2008, 21:00
Did you notice the optional second parameter of indexOf()?

jaca
28th May 2008, 21:33
The position of SPNT change depending on the file. How do I set a start if I do not know where the SPNT will appear in another file?

jaca
28th May 2008, 21:47
Actually I have a file like this:

SPNT 962
570 1485 1364 1944 2176 2417 2986 2980
3504 3362 4034 3745 4660 4176 5094 4489
5344 4600 5730 4718 8090 4962
SPNT 1122
514 1485 672 1520 1652 2111 2176 2417
2986 2980 3504 3362 4034 3745 4660 4176
5032 4433 5370 4614 5730 4718 8090 4962
SPNT 1282
514 1485 672 1520 1652 2111 2184 2431
2986 2980 3504 3362 4034 3745 4660 4176
5032 4433 5298 4593 5914 4739 8090 4962
SPNT
---------------------------------------------------------------------------------------------------------------------
And I put it in this format:

SPNT 962
VELF 570 1485 1364 1944 2176 2417 2986 2980 3504 3362
VELF 4034 3745 4660 4176 5094 4489 5344 4600 5730 4718
VELF 8090 4962
SPNT 1122
VELF 514 1485 672 1520 1652 2111 2176 2417 2986 2980
VELF 3504 3362 4034 3745 4660 4176 5032 4433 5370 4614
VELF 5730 4718 8090 4962
SPNT 1282
VELF 514 1485 672 1520 1652 2111 2184 2431 2986 2980
VELF 3504 3362 4034 3745 4660 4176 5032 4433 5298 4593
VELF 5914 4739 8090 4962

There must be an easier way that my.

jpn
29th May 2008, 05:49
The position of SPNT change depending on the file. How do I set a start if I do not know where the SPNT will appear in another file?
There are many ways to do that, really. Here's pseudo code to go through all the instances of 'SPNT' in a string list.


from = 0
index = 0
while ( index = strlist.indexOf('SPNT', from) != -1 )
...
from = index
endwhile



Actually I have a file like this:
...
And I put it in this format:
...
There must be an easier way that my.
Why don't you read it line by line and insert VELF in the beginning of line where the line doesn't start with SPNT?