Bendoon Posted January 21, 2014 Share Posted January 21, 2014 Hi I was given a assignment in college to write a "markov text generator". My code was working the other day when the errors on the server were shut off, they have however put these errors back on and I am receiving the following error: Notice: Undefined offset: 104 in /users/2017// on line 32dNotice: Undefined offset: 134 in /users/2017/ on line 32Notice: Undefined offset: 206 in /users/2017/on line 32eNotice: Undefined offset: 304 in /users/2017//markov.php on line 32eNotice: Undefined offset: 400 in /users/2017//markov.php on line 32h My code is as follows: $textarea = trim ( $_GET['textarea'] ); $textsize = $_GET['textsize']; $textarea = strtolower ( $textarea ); $string = rand(0, strlen($textarea)-1); $string = $textarea[$string]; $textarray = str_split( $textarea ); $count = 1; $newstr = ""; $position = 0; while ( $count < 200 ) { foreach ( $textarray as $char ) { if ( $char == $string ) { $string = $textarray[$position++]; $newstr .= $string; } $position++; } echo $string = $newstr[rand(0, strlen($newstr)-1)]; $count++; } Line 32 is : $string = $textarray[$position++]; Any help would be greatly appreciated Quote Link to comment Share on other sites More sharing options...
Bendoon Posted January 21, 2014 Author Share Posted January 21, 2014 (edited) ok so updated my code: $textarea = trim ( $_GET['textarea'] ); $textsize = $_GET['textsize']; $textarea = strtolower ( $textarea ); $string = rand(0, strlen($textarea)-1); $string = $textarea[$string]; $textarray = str_split( $textarea ); $count = 1; while ( $count < 200 ) { foreach ( $textarray as $k => $char ) { if ( $char == $string && array_key_exists($k++, $textarray) ) { $tempArr[]=$textarray[$k++]; } } if (count($tempArr) > 0) { echo $string=$tempArr[rand(0, count($tempArr)-1)]; } $tempArr=array(); $count++; } still geting errors this is the output : Notice: Undefined offset: 753 in /users/markov.php on line 30 Edited January 21, 2014 by Bendoon Quote Link to comment Share on other sites More sharing options...
Bendoon Posted January 21, 2014 Author Share Posted January 21, 2014 line 30 is : $tempArr[]=$textarray[$k++]; Quote Link to comment Share on other sites More sharing options...
.josh Posted January 21, 2014 Share Posted January 21, 2014 foreach ( $textarray as $k => $char ) { if ( $char == $string && array_key_exists($k++, $textarray) ) { $tempArr[]=$textarray[$k++]; } On the last iteration of the foreach loop, you will get that error, since $textarray[$k++] won't exist. For example, let's say $textarray has 10 elements (0-9). Well on the last iteration of that loop, you are attempting to assign $textarray[10] to $tempArr[] which doesn't exist. I'm not sure what the actual intent of the logic is, so I don't know if this will work for you logic-wise, but for instance to get rid of that error, you would do something like this: if (isset($textarray[$k++])) $tempArr[]=$textarray[$k++]; This will cause the code to NOT assign a new element to $tempArr on last iteration of the foreach loop, though. So like I said, I dunno if that messes with the overall goal, so maybe you need to do something else. Point is that you are attempting to access an element of $textarray that doesn't actually exist. Quote Link to comment Share on other sites More sharing options...
Bendoon Posted January 21, 2014 Author Share Posted January 21, 2014 Hi, thanks for the reply! I tried what you said : foreach ( $textarray as $k => $char ) { if ( $char == $string && array_key_exists($k++, $textarray) ) { if (isset($textarray[$k++])) { $tempArr[]=$textarray[$k++]; } } } But it gives Notice: Undefined variable: tempArr in /users/markov.php on line 37 And Notice: Undefined offset: 5 in /users/markov.php on line 32 The overall goal is create a chat bot that will take any input of text and that will chat back with random text taken from the sentences entered at the start Quote Link to comment Share on other sites More sharing options...
.josh Posted January 21, 2014 Share Posted January 21, 2014 okay so overall, these notices are telling you that you are attempting to use an index of an array that doesn't (yet) exist. So for this one: But it gives Notice: Undefined variable: tempArr in /users/markov.php on line 37 So this one looks to be coming from first iteration of your loop, that attempts to use $tempArr before you've initialized it. I see that in the bottom of your script you have $tempArr=array(); which both serves to wipe the current value and initialize it. Well you don't do that for your very first iteration. So add that to somewhere at the top of your script just before your loop. Or else move that line to inside your loop to pop when it's first used, instead of popping it after it's first used. IOW pop it at the beginning of each iteration instead of the end so that it's initialized in the beginning. Quote Link to comment Share on other sites More sharing options...
.josh Posted January 21, 2014 Share Posted January 21, 2014 basically the name of the game here is to make sure your variables, including array element references, are initialized and/or set before attempting to use them. Quote Link to comment Share on other sites More sharing options...
.josh Posted January 21, 2014 Share Posted January 21, 2014 oh also, i failed to point something out from first response: foreach ( $textarray as $k => $char ) { if ( $char == $string && array_key_exists($k++, $textarray) ) { if (isset($textarray[$k++])) { $tempArr[]=$textarray[$k++]; } } } This: $textarray[$k++] - don't do that. Instead use $textarray[$k+1] The difference is that $k++ increments $k which is screwing with your loop, seeing as how that's part of the foreach iterator. $k+1 on the other hand just takes the current value +1 instead of actually assigning a new value to $k. Quote Link to comment Share on other sites More sharing options...
Bendoon Posted January 21, 2014 Author Share Posted January 21, 2014 $textarea = trim ( $_GET['textarea'] ); $ordernumber = $_GET['ordernumber']; $textarea = strtolower ( $textarea ); $string = rand(0, strlen($textarea)-1); $string = $textarea[$string]; $textarray = str_split( $textarea ); $count = 1; $tempArr=array(); while ( $count < 200 ) { foreach ( $textarray as $k => $char ) { if ( $char == $string && array_key_exists($k++, $textarray) ) { if (isset($textarray[$k++])) { $tempArr[]=$textarray[$k++]; } } } if (count($tempArr) > 0) { echo $string=$tempArr[rand(0, count($tempArr)-1)]; } $tempArr=array(); $count++; } So this is what i have now, but the error still persists that i began with . Initializing the variable did indeed get rid of the other error Quote Link to comment Share on other sites More sharing options...
Bendoon Posted January 21, 2014 Author Share Posted January 21, 2014 yayyyy thanks a million! you fixed it . I thought that ++ and +1 were exactly the same . Thanks for helping me really appreciate it! Ill be using this site more often Quote Link to comment Share on other sites More sharing options...
Solution .josh Posted January 21, 2014 Solution Share Posted January 21, 2014 yayyyy thanks a million! you fixed it . I thought that ++ and +1 were exactly the same . Thanks for helping me really appreciate it! Ill be using this site more often Sweet.. Yeah it's only the same when you're actually assigning something. Example $k=$k+1; // <-- this increments value of $k because you are assigning $k+1 back to $k $k++; // <-- this is shorthand for the above $k+=1; // <-- also shorthand for the above ++$k; // *sort of the same. Those are all the same. But $k+1 all by itself doesn't actually assign a new value to $k. *sort of the same - Although $k++ and ++$k both increment $k, there is a subtle difference. $k++ increments after the reference to the $k is used, whereas ++$k increments before the reference to $k is used. Consider this: $x = 5; $y = 5; echo 'x: '.$x.'<br/>'; // output: 5 echo 'y: '.$y.'<br/>'; // output: 5 $a = ++$x; // this will first increment $x and then assign the value of $x to $a $b = $y++; // this will first assign the value of $y to $b and then increment $y echo 'x: '.$x.'<br/>'; // output: 6 echo 'y: '.$y.'<br/>'; // output: 6 echo 'a: '.$a.'<br/>'; // output: 6 echo 'b: '.$b.'<br/>'; // output: 5 Quote Link to comment Share on other sites More sharing options...
ignace Posted January 21, 2014 Share Posted January 21, 2014 The wonders of the internet: https://github.com/hay/markov Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.