Tsim Daim Ntawv Qhia Cov Me Nyuam Muaj Kev Tshawb Xyuas "Linux"

Thaum lub cajmeem , qhov kev hais kom ua ntawm foreach siv ib lub voj uas cov voj vev tsawm muaj kev suav qhov tseem ceeb los ntawm ib lossis ntau lub npe. Hauv kev simplest muaj ib lub voj txawv txav, varname , thiab ib daim ntawv teev, sau , uas yog ib daim ntawv teev txog qhov tseem ceeb kom muab cob rau tus menyuam npe. Lub cav sib cav yog tsab ntawv Tcl. Rau txhua lub caij ntawm daim ntawv teev npe (ua ntej dhau mus dhau), foreach assigns tus txheem ntawm lub npe rau varname li yog hais tias cov lus hais hauv lindex tau siv los rho tawm lub caij, ces hu rau Tcl txhais los ua lub cev .

Synopsis

foreach varname sau lub cev
foreach barlist1 list1 ? varlist2 list2 ... ? lub cev

Kev sib tham

Hauv rooj lus dav dav, yuav muaj ntau tshaj ib daim ntawv sau nqi (piv txwv, daim ntawv1 thiab list2 ), thiab txhua tus nqi teev npe tuaj yeem txuam nrog cov npe ntawm cov voj vev xaib (piv txwv, varlist1 thiab varlist2 ). Thaum lub sij hawm twg ntawm qhov kev hloov ntawm lub voj, cov kev hloov ntawm txhua tus ntawv teev npe yuav raug muab qhov tseem ceeb sib law liag los ntawm daim ntawv teev npe . Tus nqi hauv txhua daim ntawv yog siv los ntawm thawj zaug rau lub xeem, thiab txhua tus nqi siv raws nraim ib zaug. Tag nrho cov xov tooj ntawm cov voj vov nws loj txaus los siv tag nrho cov qhov tseem ceeb ntawm tag nrho cov nqi npe. Yog tias tus nqi sau nqi tsis muaj cov ntsiab lus txaus rau txhua qhov ntawm nws lub voj voos nyob hauv txhua lub iteration, khoob qhov tseem ceeb yog siv rau cov ntsiab lus uas ploj lawm.

Lub caij so thiab ntxiv mus cov lus yuav raug hu rau hauv lub cev , nrog tib cov nyhuv xws li hauv kev hais kom ua. Tus f los sis rov qab ua ib txoj hlua khoob.

Piv txwv

Cov voj nram qab no siv kuv thiab j raws li cov vev tse kom rov ua dua li cov npe ntawm ib daim ntawv teev.

teeb x {} foreach {ij} {abcdef} {lappend x $ j $ i} # Cov nqi ntawm x yog "badcfe" # Muaj 3 iterations ntawm lub voj.

Cov voj voog tom ntej no siv kuv thiab j kom tshaj li ob lub npe nyob rau hauv cov lus qhia.

teeb x {} foreach kuv {abc} j {defg} {lappend x $ i $ j} # Cov nqi ntawm x yog "adbecf {} g" # Muaj 4 ntawm qhov kev hloov ntawm lub voj.

Ob hom ntawv no muaj nyob rau hauv cov qauv hauv qab no.

teev x {} foreach kuv {abc} {jk} {defg} {lappend x $ i $ j $ k} # Cov nqi ntawm x yog "adebfgc {} {}" # Muaj 3 iterations ntawm lub voj.

Sau ntawv

Ob peb cov lus txib ua haujlwm nrog foreach xws li:

Vim hais tias Linux faib thiab ntsiav-tso qib sib txawv, siv tus txiv neej hais kom ua ( % txiv neej ) pom li cas ib tug hais kom ua siv rau hauv koj lub computer .