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

Re: [MacPerl] Parsing astronomical data in Perl



Brian, 

I think pack() and unpack() might do what you want, cleanly and quickly.
You can get rid of the stuff you don't want, and build a template for
your data. Saves doing repeated regexes, splits, joins, etc. You really
do have fixed length fields, which makes unpack() an good alternative.

Hope this helps,
Geoff

---begin sample code---

#!/usr/bin/perl -w

while (<DATA>) {
  @myvars = unpack('A8 A2 A3 A5 A*', $_);
  print "@myvars\n";
}



__DATA__
1234567800abcpppppqoiwencpoawien
8765432111defooooo93093fj0943

---end code---

or 

---begin sample code 2---
#!/usr/bin/perl -w

while (<DATA>) {
  ($ident, $azimuth, $decl, $elev, $junk) = unpack('A8 A2 A3 A5 A*', $_);
  print "$ident, $azimuth, $decl, $elev\n";
}



__DATA__
1234567800abcpppppqoiwencpoawien
8765432111defooooo93093fj0943

---end code---
Brian McNett wrote:

> 
> # for each line in file
> while (defined($line = <IN>)) {
>   @line = split //, $line;  # split it to a list of characters
>   unshift @line, 0;  # prepend a 0th list element to make indexes line up
> 
> # 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/;

<snip>

> # and so forth...
> # once you get up to where existance of field is not guaranteed,
> # check that the line is sufficiently long to have the field
> # Actually this test starts around the 147th char.

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