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

Re: [MacPerl] "rewinding" a file



At 09:52 20.06.96, Hal Wine wrote:

>Here's the approach I'd use:
>- keep the choosen records in an associative array
>- use the keyfield as the key (making it unique if need be)

[...]

>while( <> ) {
>    if( /(key)whatever/ ) {
>        $key = $1; # or whatever
>        # you only need the following 3 lines if keys aren't unique
>        $keyindex = 0;
>        $keyindex++ while defined( $a{ "$1$keyindex" } );
>        $key .= $keyindex;
>        # end of non-unique key handling
>
>        $a{ $key } = $_; # or whatever part of record you want
>    }
>}

I'm a Perl novice, but this is not strictly true. Nothing wrong with the
programming as far as I know, but the method of ensuring that the keys are
unique is flawed.

You can't separate the key from the keyindex, unless you are sure that that
the keys never are numbers. If the first key is "11", and the eleventh is
"1", both will end up as "110".

Either you'll have to use index numbers (or a separator) from outside the
valid alphabet of the keys, or you'll have to use a fixed number of
characters.

Jonny