473,499 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tokenizing a token

I'm writing a snippet of code intended to parse a grid that is
represented by a long string. Each row of the grid is deliminated in
the string with a '~' character. Inside each row substring, columns are
deliminated with a '|' character. I'm therfore trying to use nested
strtok() calls as follows:
$this->grid = array();
$rowTokenizer = strtok($gridString, "~");
$rowIndex = 0;
while($rowTokenizer) {
$colIndex = 0;
$columnTokenizer = strtok($rowTokenizer, "|");
while($columnTokenizer) {
$this->grid[rowIndex][colIndex] = $columnTokenizer;
$colIndex++;
$columnTokenizer = strtok("|");
}
$rowIndex++;
$rowTokenizer = strtok("~");
}
However, this code does not generate a proper grid. I simplifed the
operation just to pring out each row-substring so I could see what's
going on inside:
$rowTokenizer = strtok($gridString, "~");
while($rowTokenizer) {
echo $rowTokenizer . "<br/>";
$columnTokenizer = strtok($rowTokenizer, "|");
while($columnTokenizer) {
$columnTokenizer = strtok("|");
}
$rowTokenizer = strtok("~");
}
... and here's what the output looks like:
#|1|#|#|#|#|2|#|#|#|#|3|#|#
1|#|#|#|#|2|#|#|#|#|3|#|#
#|#|#|#|2|#|#|#|#|3|#|#
#|#|#|2|#|#|#|#|3|#|#
#|#|2|#|#|#|#|3|#|#
#|2|#|#|#|#|3|#|#
2|#|#|#|#|3|#|#
#|#|#|#|3|#|#
#|#|#|3|#|#
#|#|3|#|#
#|3|#|#
3|#|#
#|#
#
It's never making it past the first row. For each interation of the
outer while() loop, $rowTokenizer is simply representing the first row
with the left-most column token stripped out. Can anyone spot what I'm
doing wrong, and how I can sucessfully tokenize a token without
interfering with the outermost tokenization process? Thanks in advance!
Jul 17 '05 #1
2 2095
How about:

$someString; //the input string

$someArray = explode('~', $someString); //$someArray now has the 'rows'

$numrows = count($someArray);
for($i = 0; $i < $numrows; $i++)
$someArray[$i] = explode('|', $someArray[$i]);
//$someArray is now an array of arrays

Jul 17 '05 #2
ZeldorBlat wrote:
How about:

$someString; //the input string

$someArray = explode('~', $someString); //$someArray now has the 'rows'

$numrows = count($someArray);
for($i = 0; $i < $numrows; $i++)
$someArray[$i] = explode('|', $someArray[$i]);
//$someArray is now an array of arrays

Thanks a ton, that fixed my problem! I'd still love to understand the
problem better though, I get the feeling that my understanding of PHP
string tokenization isn't as strong as it should be.

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
6301
by: Simon Morgan | last post by:
I'm trying to write a function to parse a Reverse Polish Notation string from stdin and return 1 token at a time. For those of you who are unaware an RPN string looks like this: 1 2 + 4 * 3 + ...
3
16188
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
11
3911
by: wreckingcru | last post by:
I'm trying to tokenize a C++ string with the following code: #include <iostream> #include <stdio> #include <string> using namespace std; int main(int argc, char* argv)
12
2038
by: Joriveek | last post by:
Hi, I have a little piece of program here Basically what it does is, it copies the strings of variable widths. The basis is until it finds a comma ",". The input is a CSV/Comma Separated...
8
2154
by: Acolyte | last post by:
Ok, the program I'm working on now involves taking an input string and tokenizing it, by seperating it by spaces. Here's what I've got: #include <stdio.h> #include <string.h> int main (void) {...
8
2751
by: arnaudk | last post by:
Hello, I need to get the following string "12332321213,SomeText,3.141592654" into constituent variables: an unsigned long int, a string and a double. So far I have, string line =...
4
1405
by: dhirenved | last post by:
hi, i have to use string tokenizing to isolate commands and the arguments that i have to execute using the execvp command. here is what i'm using char* token; char buf; printf("enter command");...
10
1549
by: Jonathan Sion | last post by:
Hi, I got a large text string, and a bunch of regular expression patterns i need to find within the string. in other words, to find all the 'tokens' inside it. of course I could use the regexp...
0
922
by: Linarexol | last post by:
just a quick question, does anyone know how to write a regexp for tokenization??????????? to get all email addresses and abbreviations as 1 token? thanks heaps...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.