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

Re: [MacPerl] Parsing astronomical data in Perl



At 12.06 -0500 1998.12.21, Brian McNett wrote:
># dig out the fields we need
>  $ident = join '', @line[1..8];   # identifier of star
>  $ident =~ s/\s*(.*?)\s*/$1/;
>  $comp = join '', @line[9..10];   # number of components
>  $comp =~ s/\s*(.*?)\s*/$1/;
>  $distrel = $line[11];            # reliability of distance (ref1)
>  $RAh = join '', @line[13..14];   # right ascension (hours)
>  $RAh =~ s/\s*(.*?)\s*/$1/;
>  $RAm = join '', @line[16..17];   # right ascension (minutes)
>  $RAm =~ s/\s*(.*?)\s*/$1/;
>  $RAs = join '', @line[19..20];   # right ascension (seconds)
>  $RAs =~ s/\s*(.*?)\s*/$1/;
>  $DEd = join '', @line[22..24];   # declination (degrees + sign)
>  $DEd =~ s/\s*(.*?)\s*/$1/;

FWIW, I would replace this whole thing with a data structure and subroutine
like so:

%data = (
    ident       => [1,8],
    comp        => [9,10],
    distrel     => 11,
    RAh         => [13,14],
    # etc.
);

sub get_data {
    my $line = shift;
    my %line;
    foreach my $key (keys %data) {
        if (ref($data{$key})) {
            my @arr = @{$data{$key}};
            $line{$key} = substr($line, $arr[0], $arr[1] - $arr[0]);
            $line{$key} =~ s/^\s*//;
            $line{$key} =~ s/\s*$//;
        } else {
            $line{$key} = substr($line, $data{$key}, 1);
        }
    }
    returrn(%line);  # might have to massage output, or adjust routines
                     # that deal with data to deal with, e.g., $line{DEd}
                     # instead of $DEd
}

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch