Newbie Question : "grep"

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Mon Mar 12 13:55:20 EDT 2007


On Mar 12, 10:01 am, "Erik Johnson" <nob... at invalid.com> wrote:
> Sorry, I forgot to paste the modified version of my code in the post:. I
> think this is the same behaviour:
>
> for line in lines:
>     if "placed" in line:
>         if "i_a/i_b/ROM/" in line:
>             pos = (line.split()[4]).split("_")[1]
>             found = False
>
>             for (tag, (start, end)) in tags:
>                 if tag in line:
>                     found = True
>                     print "        i_a/i_b/ROM/%s [%i:%i] LOC =" %\
>                         (pos, tag, start, end)
>                     break
>
>             if not found:
>                 print "Error"



Instead of using a sentinal value (i.e., found),
one can use the 'else' clause of a 'for' loop...


pos = (line.split()[4]).split("_")[1]
for (tag, (start,end)) in tags:
    if tag in line:
        print "    i_a/i_b/ROM/%s [%i:%i] LOC=%s" %\
            (pos,tag,start,end)
        break
else
    print "Error"

--
Hope this helps,
Steven




More information about the Python-list mailing list