Banner
Views: 236,704,572
Time: 2013-05-25 10:40:10 AM
31 users online: aj6666, Dark Mario Bros, o dax, DragezeeY, everest700, Falconpunch, Gloomy Star, Grav, GreenHammerBro, o grishnax, o HuFlungDu, HyperMario, jellybean, Kazeshin, kianworld, KirbyPizzaPalace, levelengine, Lightvayne, Luckiest3, Mister Kirby, MrL, Pokeymeister80, Rextep, o RPG Hacker, o S.N.N., o ShadowPhoenix, shreerocks1324, Silver Scarlet, o Smallhacker, SMW_Forever, willj168 - Guests: 32 - Bots: 11Users: 22,886 (1,282 active)
Latest: ChiLL3X
Tip: Try to make a hack that is both hard enough to be challenging and easy enough to be fun.
Text parsing tricks?
Forum Index - Hobbies - Computers & Technology - Text parsing tricks?
Pages: « 1 »
Lets just say that I have a string of an arbitrary length that's set up like "x=####y=####" where #### is a numeric value of an unknown length that I need to store to an integer variable. I have no way of knowing how long the numbers are, but I do know their start positions. Is the fastest way really to just loop through every character until I come across a non-numeric one, or is there a faster way?

The only programming class available to me didn't really explain much beyond "This is an 'if' statement. This is a 'while' statement. Visual Basic is an awesome language and C++ doesn't exist." etc., so yeah, I'm kind of weak when it comes to basic stuff like this.
Hmm... I don't know how C++'s regular expression matching works, but it should be possible to use that. For instance, in Python the code would look like this (Where numstring is the whole string and index is the start position of the current number you are editing):

Code
import re tmpstring = numstring[index:] match = re.match("[0-9]*", tmpstring) numstr = match.group(0) num = int(numstr)


In that example, numstr would now be the string representation of the number (i.e. if you had "x=1234", numstr would be "1234"), then it should be a simple transformation to change it form a string to an int, which is what I made num to be, I'm not sure how that is done in C++. Of course the whole thing can be shrunk to two lines:

Code
import re num = int(re.match("[0-9]*", numstring[index:]).group(0))


But I felt the other way was easier to understand, if a bit slower.

Anyways, since Python is built on C, that should all be doable pretty easily with C++ (Though I think you need boost:: to get regular expressions).

Originally posted by Kipernal
Visual Basic is an awesome language

Yeah, it's times like that I'm glad my college uses only libre software.
Last edited on 2011-04-24 11:07:44 AM by HuFlungDu.
You will need to check every character, yes. The question is if you're doing it manually or if you're having a regex engine do it for you.

Either way, assuming you can't/don't want to use regex, there is still a faster way to test a character against a range of characters than to try every character in the range until a match is found or you run out of characters.

You can convert your characters to their numeric value and do a simple lesser than, greater than test.

For instance: "0" has the ASCII value 48 and "9" is 57. To test if a character is numeric, you could do:

Code
Dim asciiValue As Integer = Asc(char) If asciiValue >= 48 And asciiValue <= 57 Then /* insert blah here */ End If

DISCLAIMER: I don't actually know VB. This was my best guess from what I could quickly read up on.
You can use a Finite State Machine (which is looping through every character, but a pretty "conventional" way to do it). It should actually be more efficient than regular expressions which have to loop through every character anyway.
I don't know VisualBasic, so I'll write it in Java. I haven't tested if this will compile, but the basic idea should work.

Code
int state = 0 string currentName = ""; string currentValue = ""; for (int i = 0; i < yourString.length(); i++) { char ch = yourString.charAt(i); if (state == 0) { if (ch == '=') state = 1; else currentName += ch; } else if (state == 1) { if (Character.isDigit(ch)) currentValue += ch; else { handleValue(currentName, currentValue); state = 0; currentName = ch; currentValue = ""; } } } if (!currentName.Equals("")) handleValue(currentName, currentValue);



where handleValue() is a method that does something with each of your values. You don't have to actually use a method, I just used one for simplicity. You'll probably want to parse an integer out of each of your numerical values.
Last edited on 2011-04-24 10:02:51 PM by edit1754.
Code
int GetNumber(char* str, int start) { int n = 0; for (char x = str[start]; x && x >= '0' && x <= '9'; x = str[start++]) n = (n * 10) + x - '0'; return n; }

Not sure if that need an explanation.
Pages: « 1 »
Forum Index - Hobbies - Computers & Technology - Text parsing tricks?

The purpose of this site is not to distribute copyrighted material, but to honor one of our favourite games.

Copyright © 2005 - 2013 - SMW Central
Legal Information - Link To Us


Total queries: 29

Menu