Tokenizing a token

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    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($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!
  • ZeldorBlat

    #2
    Re: Tokenizing a token

    How about:

    $someString; //the input string

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

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

    Comment

    • Steve

      #3
      Re: Tokenizing a token

      ZeldorBlat wrote:[color=blue]
      > How about:
      >
      > $someString; //the input string
      >
      > $someArray = explode('~', $someString); //$someArray now has the 'rows'
      >
      > $numrows = count($someArra y);
      > for($i = 0; $i < $numrows; $i++)
      > $someArray[$i] = explode('|', $someArray[$i]);
      > //$someArray is now an array of arrays
      >[/color]


      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.

      Comment

      Working...