Re: ArrayInterator always true

From: Date: Fri, 27 Jan 2012 22:10:58 +0000
Subject: Re: ArrayInterator always true
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-316418@lists.php.net to get a copy of this message
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@gmail.com> 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

« previous php.general (#316418) next »