Combining String data
Posted
#1
(In Topic #1107)
Enthusiast

Just wanted to run a quick problem / question past you all
I have a data file that holds data in the following format
barcode|qty (eg 50201600|1)
What I would like to do is is read the file in (as this can be anything from 1 line to 5,000 lines) and I would like to combine the qtys for items that have already been read in
What would be the best way to do that? Can I do it with a Array or does it have to be a database table?
For example
5018374320766|1
50201600|2
99998|1
99999|2
5018374320766|2
50201600|1
so when it is out putted I would get
5018374320766|3
50201600|3
99998|1
99999|2
Posted
Regular

Posted
Guru

Code (gambas)
- ' Gambas class file
- 'Contents of file BCode.txt
- ' 5018374320766|1
- ' 50201600|2
- ' 99998|1
- ' 99999|2
- ' 5018374320766|2
- ' 50201600|1
- ' 5018374320766|5
- ' 99999|2
- ' 547892|9
- ' 99999|2
- ' 547892|5
- sList.Clear 'Clear sList
- ' Output
- ' 5018374320766|8
- ' 50201600|3
- ' 547892|14
- ' 99998|1
- ' 99999|6
Posted
Regular

Code (gambas)
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

Code (gambas)
- ' Gambas module file
- SumOverBCode(sLines)
- $totals.Add(0, aWorkline[0])
- $totals[aWorkline[0]] += aWorkline[1]
Output:
5018374320766|8
50201600|3
99998|1
99999|6
547892|14
Total duration = 179ms
Posted
Guru

A couple of points.
vuott: - Your code works perfectly if line 22 ss.Remove(ss.Max, 1) is removed!
thatBruce: - Your code will look better if you use the gb rather than the </> button. (I have edited your post above).
<IMG src="https://www.cogier.com/gambas/gb_button.png">
</IMG>
Posted
Regular

The text file, which I load, is structured exactly like this:cogier said
vuott: - Your code works perfectly if line 22 ss.Remove(ss.Max, 1) is removed!
<COLOR color="#800000">5018374320766|1
50201600|2
99998|1
99999|2
5018374320766|2
50201600|1
5018374320766|5
99999|2
547892|9
99999|2
547892|5</COLOR>
If I do not enter the command line "ss.Remove(ss.Max, 1) ", I get the error "Out of Bound ".
However, I can remove it, but I have to add the RTrim() function to the string assignment in the first declaration of the local variable 's'.
If not, I still get the error "Out of Bound ".
Code (gambas)
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

If that is the case then I find the best way to to handle that is in the initial file load when the file string is split by line endings, just by ignoring nulls in the split.
Posted
Guru

thatbruce said
At a guess, I'd say that one of you has a blank line at the end of the file and one doesn't. In other words a Cr or whatever on the last data line.
If that is the case then I find the best way to to handle that is in the initial file load when the file string is split by line endings, just by ignoring nulls in the split.
I agree and that is what is in my code. Below is one change I have made to vuott's code that deals with the 'extra' lines.
Code (gambas)
1 guest and 0 members have just viewed this.


