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($gridStr ing, "~");
$rowIndex = 0;
while($rowToken izer) {
$colIndex = 0;
$columnTokenize r = strtok($rowToke nizer, "|");
while($columnTo kenizer) {
$this->grid[rowIndex][colIndex] = $columnTokenize r;
$colIndex++;
$columnTokenize r = 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($gridStr ing, "~");
while($rowToken izer) {
echo $rowTokenizer . "<br/>";
$columnTokenize r = strtok($rowToke nizer, "|");
while($columnTo kenizer) {
$columnTokenize r = 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!
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($gridStr ing, "~");
$rowIndex = 0;
while($rowToken izer) {
$colIndex = 0;
$columnTokenize r = strtok($rowToke nizer, "|");
while($columnTo kenizer) {
$this->grid[rowIndex][colIndex] = $columnTokenize r;
$colIndex++;
$columnTokenize r = 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($gridStr ing, "~");
while($rowToken izer) {
echo $rowTokenizer . "<br/>";
$columnTokenize r = strtok($rowToke nizer, "|");
while($columnTo kenizer) {
$columnTokenize r = 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!
Comment