Re: ArrayInterator always true

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I've got a temporary fix but still the iterator always return true,
still hope someone could help me out to point out the problem:)

//parseOptions utilitiese by tgckpg
function parseOptions ( $argStream, $handler ) {
	//Chop first useless argument -- argv[0]
	array_shift ( $argStream ) ;
	//Initiate ArrayObject for iterator
	$arrayobject = new ArrayObject ( $argStream ) ;
	//Initiate iterator for iteration
	$iterator = $arrayobject->getIterator();

	//If options is set first
	if( $iterator->valid() && preg_match ( '/^-\w$/', $iterator->current() ) ) {
		//iterate through whole argument stream
		for (	; $iterator->valid(); $iterator->next() ) {
			//Check if reached next option
			if( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
				//Get current options
				$currOpt = $opts;
				//echo "$currOpt\n";
				//Test if next stream is an option
				for ( $iterator->next(); $iterator->valid(); $iterator->next() ) {
					if ( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
						//echo "$currOpt $opts\n";
						$handler($currOpt);
						$currOpt = $opts;
					} else break;
					//var_dump($iterator->valid());
				}
			}//End if
			//echo "$currOpt $opts\n";
			$handler($currOpt, $opts);
			//A temporary fix for infinite loop<------------------------------
			if(!$iterator->valid())
				break;
		}// End for
	//If option is not set first.
	} else {
		//Try other approach.
	}// End if
}

On Tue, Jan 24, 2012 at 4:18 AM, TCP <tgckpg@xxxxxxxxx> wrote:
> I'm trying to parse an $agrv array that contain options (without
> square brackets): [-a "abc" -b "bbc" "bcc" -d "dbc" -e -f]
> I use ArrayIterator to iterate through the line:
>  - whenever it reach /-\w/, it read through the following qoutes
> until it reach another /-\w/.
>
>
>
> The problem is it seems the $iterator->valid() always return TRUE and
> cause infinte loop.
>
>
> function parseOptions ( $argStream, $handler ) {
>        //Chop first useless argument -- argv[0]
>        array_shift ( $argStream ) ;
>        //Initiate ArrayObject for iterator
>        $arrayobject = new ArrayObject ( $argStream ) ;
>        //Initiate iterator for iteration
>        $iterator = $arrayobject->getIterator();
>
>        //If options is set first
>        if( $iterator->valid() && preg_match ( '/^-\w$/', $iterator->current() ) ) {
>                //iterate through whole argument stream
>                for (   ; $iterator->valid(); $iterator->next() ) {
>                        //Check if reached next option
>                        if( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
>                                //Get current options
>                                $currOpt = $opts;
>                                //echo "$currOpt\n";
>                                //Test if next stream is an option
>                                for ($iterator->next(); $iterator->valid(); $iterator->next() ) {
>                                        if ( preg_match ( '/^-\w$/', $opts = $iterator->current() ) ) {
>                                                //echo "$currOpt $opts\n";
>                                                //$handler($currOpt, $opts);
>                                                $currOpt = $opts;
>                                        }
>                                        var_dump($iterator->valid());
>                                }
>                        }//End if
>                        //echo "$currOpt $opts\n";
>                        //$handler($currOpt, $opts);
>                }// End for
>
>        //If option is not set first.
>        } else {
>                //Try other approach.
>        }// End if
> }
>
>
> I've no idea what is going on.
> Please help.
>
>
> Regards,
> Panguin
>
> --
> 筆使文富,卻使人窮。



-- 
Regards,
Panguin



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux