Perl and Python, a practical side-by-side example.

John Machin sjmachin at lexicon.net
Sat Mar 3 04:03:08 EST 2007


On Mar 3, 7:08 pm, attn.steven.... at gmail.com wrote:
> On Mar 2, 2:44 pm, "Shawn Milo" <S... at Milochik.com> wrote:
>
> (snipped)
>
> > I'm attaching both the Perl and Python versions, and I'm open to
> > comments on either. The script reads a file from standard input and
> > finds the best record for each unique ID (piid). The best is defined
> > as follows: The newest expiration date (field 5) for the record with
> > the state (field 1) which matches the desired state (field 6). If
> > there is no record matching the desired state, then just take the
> > newest expiration date.
>
> > Thanks for taking the time to look at these.
>
> My attempts:
>
>
> ### Python (re-working John's code) ###
>
> import sys
>
> def keep_best(best, current):
>
>     ACTUAL_STATE = 1
>     # John had these swapped
>     DESIRED_STATE = 5
>     EXPIRY_DATE = 6

*Bullshit* -- You are confusing me with Bruno; try (re)?reading what
the OP wrote (and which you quoted above):
"""
The newest expiration date (field 5) for the record with
the state (field 1) which matches the desired state (field 6).
"""

and his code (indented a little less boisterously):

"""
#If the current record is the correct state
if current.split("\t")[1] == current.split("\t")[6]:
    #If the existing record is the correct state
    if best.split("\t")[1] == best.split("\t")[6]:
        #If the new record has a newer exp. date
        if current.split("\t")[5] > best.split("\t")[5]:
"""




More information about the Python-list mailing list