inputting the ephemerides

T

Tim Greer

Larry said:
I read this in a footnote while my pastor was giving the sermon today.
From her perspective, she can't know whether I'm reading the camel
book or the bible.

Bottom of page 35, the asterisk reads "A good source of information on
regular expressions is Jeffrey Friedl's book, _Mastering Regular
Expressions_ (O'Reilly and Associates).

What version would I want and how much should I expect to pay for a
used
one? I bought a Harbison and Steele from someone off the net, and the
s.o.b. sends me a 1985 version, older than K&R 1. I was so disgusted
that I quit C for a year.

Amazon and B&N both have used books or sale (and I'm sure other sites,
too). Just compare that to the current edition out and see what one's
you can grab, if you want a used one.
 
L

Larry Gates

Amazon and B&N both have used books or sale (and I'm sure other sites,
too). Just compare that to the current edition out and see what one's
you can grab, if you want a used one.

What's New
New in the Third Edition are a new chapter on PHP (and upgraded PHP
coverage throughout the core chapters), and a completely rewritten Java
chapter to reflect changes from Java 1.4.0 to Java 1.5/1.6. Otherwise,
there are only minor updates and typo fixes. (For example, if your interest
is Perl or .NET, there's little new in the Third Edition that's not in the
Second Edition.)

Amazon sells the new one for 29.69 while used ones begin at $25.-. Unless
someone has edition 2 for twenty bucks, I think this is a buy.
 
T

Tim Greer

Larry said:
What's New
New in the Third Edition are a new chapter on PHP (and upgraded PHP
coverage throughout the core chapters), and a completely rewritten
Java chapter to reflect changes from Java 1.4.0 to Java 1.5/1.6.
Otherwise, there are only minor updates and typo fixes. (For example,
if your interest is Perl or .NET, there's little new in the Third
Edition that's not in the Second Edition.)

Right, the newest might not be relevant to your needs. I wouldn't know
the differences in them, my copy is very old and I really never needed
to reference it (regex's come naturally to me -- unless I make a stupid
typo in response to a usenet post from not paying attention).
 
P

Peter J. Holzer

my @s = split / /, $line; [...]

So now I want to take the second element of s and hit it with

s2 = s/h/ /;

, but I can't figure out how to deal with these perl arrays like I'm
accustomed to with C or fortran.

Neither in C nor in Fortran do you access the second element of an array
named "s" with "s2". In C it's "s[1]" and in Fortran it's "s(2)". I was
going to suggest you should read perldoc perldata, but I see in a
different message that you're reading the Perl bible in church, so I'm
suggesting you read the chapter about arrays in the camel book instead.
I'm sure it explains in detail how to access array elements.

hp
 
P

Peter J. Holzer

I bought a Harbison and Steele from someone off the net, and the
s.o.b. sends me a 1985 version, older than K&R 1.

K&R 1 was published in 1978, IIRC (the German translation is from 1983).

hp
 
L

Larry Gates

my @s = split / /, $line; [...]

So now I want to take the second element of s and hit it with

s2 = s/h/ /;

, but I can't figure out how to deal with these perl arrays like I'm
accustomed to with C or fortran.

Neither in C nor in Fortran do you access the second element of an array
named "s" with "s2". In C it's "s[1]" and in Fortran it's "s(2)". I was
going to suggest you should read perldoc perldata, but I see in a
different message that you're reading the Perl bible in church, so I'm
suggesting you read the chapter about arrays in the camel book instead.
I'm sure it explains in detail how to access array elements.

hp

Thanks, peter, this puts me back on track. Perldoc perldata *and* the
camel book were both helpful. When I'm confused, sometimes the thing I
post is my last and weakest try at doing something. I did use the correct
$s[2] formulation that one would imagine from a child language of C, but
there was nothing there to look at.

Page 119 of the camel book has a section on looping for folks whose
background is C or Java, and the next page shows the equivalent perl idiom.

The good news is that I am splitting in a predictable, usable way. The bad
news is that I've got 23 fields instead of the ten that I want:

use strict;
use warnings;


my $filename = 'eph6.txt';
my $filename2 = 'outfile1.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
open(my $gh, $filename2) or die "cannot open $filename2: $!";

while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}
close($fh);

seek($gh,0,0);

while (my $line = <$gh>) {

my @s = split / /, $line;

for my $i (0..22) {
print STDOUT "s $i is $s[$i]\n";
}
}
close($gh);
# perl reg8.pl


C:\MinGW\source> perl reg8.pl
s 0 is Sun
s 1 is
s 2 is
s 3 is 19h
s 4 is 43m
s 5 is 51s
s 6 is
s 7 is
s 8 is -21
s 9 is
s 10 is 17.8'
s 11 is
s 12 is
s 13 is 0.984
s 14 is
s 15 is
s 16 is -35.020
s 17 is
s 18 is
s 19 is 87.148
s 20 is
s 21 is
s 22 is Set
....
s 0 is Pluto
s 1 is
s 2 is
s 3 is 18h
s 4 is 6m
s 5 is 40s
s 6 is
s 7 is
s 8 is -17
s 9 is
s 10 is 44.9'
s 11 is
s 12 is
s 13 is 32.485
s 14 is
s 15 is
s 16 is -52.833
s 17 is
s 18 is
s 19 is 108.052
s 20 is
s 21 is
s 22 is Set

C:\MinGW\source>

Can I make a better split to include fields with only digits or characters?
 
L

Larry Gates

K&R 1 was published in 1978, IIRC (the German translation is from 1983).

hp

Ok. I've got the 2te Ausgabe of _Programmerieren in C_ from 1990. It only
gives dates from the original Ami versions: 1988, 1978.

I do remember that C was very much the hot topic when I was in Germany from
85-87. I hadn't heard of it in the states.

The H&S from '85 is completely unusable for my purposes. I've been burning
books based on lack of relevance to C. That one won't survive the heating
season.
 
J

Jürgen Exner

Larry Gates said:
while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}

What is the section above supposed to do?
It reads a line from a file into memory, alters the line, and then
happily throws it away by reading the next line without doing anything
with that altered line. This is a giant NOOP. Why are you doing that?
seek($gh,0,0);

while (my $line = <$gh>) {
my @s = split / /, $line; [...]
C:\MinGW\source> perl reg8.pl
s 0 is Sun
s 1 is
s 2 is
s 3 is 19h
s 4 is 43m
s 5 is 51s
s 6 is
s 7 is
[...]
Can I make a better split to include fields with only digits or characters?

Apparently your original string contains consecutive sequences of space
characters. Your RE will split between each individual space, thus
creating numerous empty result strings.
If you don't want that then instead of splitting at a single space use
the whole consecutive sequence of space characters as the item to split
the original string at.

jue
 
S

sln

Ok. I've got the 2te Ausgabe of _Programmerieren in C_ from 1990. It only
gives dates from the original Ami versions: 1988, 1978.

I do remember that C was very much the hot topic when I was in Germany from
85-87. I hadn't heard of it in the states.

I used to program in Bcpl (aka B) before C. Was too young to remember A.

sln
 
J

Jim Gibson

Larry Gates said:
my @s = split / /, $line; [...]

So now I want to take the second element of s and hit it with

s2 = s/h/ /;

, but I can't figure out how to deal with these perl arrays like I'm
accustomed to with C or fortran.

Neither in C nor in Fortran do you access the second element of an array
named "s" with "s2". In C it's "s[1]" and in Fortran it's "s(2)". I was
going to suggest you should read perldoc perldata, but I see in a
different message that you're reading the Perl bible in church, so I'm
suggesting you read the chapter about arrays in the camel book instead.
I'm sure it explains in detail how to access array elements.

hp

Thanks, peter, this puts me back on track. Perldoc perldata *and* the
camel book were both helpful. When I'm confused, sometimes the thing I
post is my last and weakest try at doing something. I did use the correct
$s[2] formulation that one would imagine from a child language of C, but
there was nothing there to look at.

Page 119 of the camel book has a section on looping for folks whose
background is C or Java, and the next page shows the equivalent perl idiom.

The good news is that I am splitting in a predictable, usable way. The bad
news is that I've got 23 fields instead of the ten that I want:

use strict;
use warnings;


my $filename = 'eph6.txt';
my $filename2 = 'outfile1.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
open(my $gh, $filename2) or die "cannot open $filename2: $!";

while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}
close($fh);

seek($gh,0,0);

while (my $line = <$gh>) {

my @s = split / /, $line;

for my $i (0..22) {
print STDOUT "s $i is $s[$i]\n";
}
}
close($gh);
# perl reg8.pl
C:\MinGW\source>

Can I make a better split to include fields with only digits or characters?

Change your split line to:

my @s = split ' ', $line;

See 'perldoc -f split:

As a special case, specifying a PATTERN of space (' ') will
split on white space just as "split" with no arguments does.
Thus, "split(' ')" can be used to emulate awk's default
behavior, whereas "split(/ /)" will give you as many null
initial fields as there are leading spaces. A "split" on
"/\s+/" is like a "split(' ')" except that any leading
whitespace produces a null first field. A "split" with no
arguments really does a "split(' ', $_)" internally.

And

my @s = split(/\s+/,$line)

also works in your case, but will give you a leading empty field if
$line begins with spaces.
 
J

Jim Gibson

I've got 2 questions:

q1) How do I loop over @s and print them out with a delimiter to see if I
have what I think I have? One response is, "how many times do we have to
tell you this?" I think the answer is at least 3; I can't figure it out
and have tried serially.

print join(',',@s), "\n";
q2) How do I write to $gh and replace thw entire file, even if there are
fewer characters in the replacer?

Open another file for writing and write the modified lines to that
file. When you are all done, you can rename the new file to the name of
the old file, thereby deleting the old file, or you can just use the
new file instead.
 
T

Tad J McClellan

my @s = split / /, $line;

Can I make a better split to include fields with only digits or characters?


Yes. And the "better split" was already provided in a different thread.

Does this sound familiar?

You are using the pattern / / which will not work correctly...

will have *two* spaces in front of it. You need to use the
special split pattern ' ' instead:

perl -le'print for ( split " ", localtime 1231358400 )[1,2,4]'


ie.

my @s = split ' ', $line;
or
my @s = split / +/, $line;
or
my @s = split /\s+/, $line;

as appropriate.
 
T

Tad J McClellan

Jim Gibson said:
Open another file for writing and write the modified lines to that
file. When you are all done, you can rename the new file to the name of
the old file, thereby deleting the old file, or you can just use the
new file instead.


Or, probably best yet, let Perl's inplace editing feature handle all of
that administrivia for you.

See the -i switch in

perldoc perlrun

and the $^I variable in

perldoc perlvar
 
L

Larry Gates

What is the section above supposed to do?
It reads a line from a file into memory, alters the line, and then
happily throws it away by reading the next line without doing anything
with that altered line. This is a giant NOOP. Why are you doing that?

I've been doing this in bite-sixed chunks. The problems that dogged me for
a long time were what to do with tabs, the ER that followed the moon's
distance from earth and no other body's, and the degree sign, which is B0
hex and therefore not ascii.

I don't know what noop is, but I suspect it's a loop that does a whole lot
of nothing. (Douglas Hofstadter had his own clever alterations along these
lines, but that's a different story.)

I *do* use the $line, because that's what I split on:

my @s = split /\s+/, $line;

Apparently your original string contains consecutive sequences of space
characters. Your RE will split between each individual space, thus
creating numerous empty result strings.
If you don't want that then instead of splitting at a single space use
the whole consecutive sequence of space characters as the item to split
the original string at.

jue

Ok. It sometimes takes me a while to use some of the suggestions that you
(plural) give me. I was finally able to mimic the join syntax that you
(singular) posted at the top of this subthread, and I'm pleased to roll out
my first output that I can call a solution:

use strict;
use warnings;


my $filename = 'eph6.txt';
my $filename2 = 'outfile1.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
open(my $gh, $filename2) or die "cannot open $filename2: $!";

while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}
close($fh);

seek($gh,0,0);

while (my $line = <$gh>) {
my @s = split /\s+/, $line;

$s[1] =~ s/h//;
$s[2] =~ s/m//;
$s[3] =~ s/s//;
$s[5] =~ s/'//;
for my $i (0..9) {
print STDOUT "s $i is $s[$i]\n";
}
$line = join(' ', @s[0..$#s]);
print "$line\n";
}
close($gh);
# perl reg9.pl

C:\MinGW\source>perl reg9.pl
s 0 is Sun
s 1 is 19
s 2 is 43
s 3 is 51
s 4 is -21
s 5 is 17.8
s 6 is 0.984
s 7 is -35.020
s 8 is 87.148
s 9 is Set
Sun 19 43 51 -21 17.8 0.984 -35.020 87.148 Set
....
s 0 is Pluto
s 1 is 18
s 2 is 6
s 3 is 40
s 4 is -17
s 5 is 44.9
s 6 is 32.485
s 7 is -52.833
s 8 is 108.052
s 9 is Set
Pluto 18 6 40 -17 44.9 32.485 -52.833 108.052 Set

C:\MinGW\source>
--
larry gates

Are you going to bother to set up an unspoofable identity for every
shirt in your closet?
-- Larry Wall, 8th State of the Onion
 
S

sln

What is the section above supposed to do?
It reads a line from a file into memory, alters the line, and then
happily throws it away by reading the next line without doing anything
with that altered line. This is a giant NOOP. Why are you doing that?

I've been doing this in bite-sixed chunks. The problems that dogged me for
a long time were what to do with tabs, the ER that followed the moon's
distance from earth and no other body's, and the degree sign, which is B0
hex and therefore not ascii.

I don't know what noop is, but I suspect it's a loop that does a whole lot
of nothing. (Douglas Hofstadter had his own clever alterations along these
lines, but that's a different story.)

I *do* use the $line, because that's what I split on:

my @s = split /\s+/, $line;

Apparently your original string contains consecutive sequences of space
characters. Your RE will split between each individual space, thus
creating numerous empty result strings.
If you don't want that then instead of splitting at a single space use
the whole consecutive sequence of space characters as the item to split
the original string at.

jue

Ok. It sometimes takes me a while to use some of the suggestions that you
(plural) give me. I was finally able to mimic the join syntax that you
(singular) posted at the top of this subthread, and I'm pleased to roll out
my first output that I can call a solution:

use strict;
use warnings;


my $filename = 'eph6.txt';
my $filename2 = 'outfile1.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
open(my $gh, $filename2) or die "cannot open $filename2: $!";

while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}
close($fh);

seek($gh,0,0);

while (my $line = <$gh>) {
my @s = split /\s+/, $line;

$s[1] =~ s/h//;
$s[2] =~ s/m//;
$s[3] =~ s/s//;
$s[5] =~ s/'//;
for my $i (0..9) {
print STDOUT "s $i is $s[$i]\n";
}
$line = join(' ', @s[0..$#s]);
print "$line\n";
}
close($gh);
# perl reg9.pl

C:\MinGW\source>perl reg9.pl
s 0 is Sun
s 1 is 19
s 2 is 43
s 3 is 51
s 4 is -21
s 5 is 17.8
s 6 is 0.984
s 7 is -35.020
s 8 is 87.148
s 9 is Set
Sun 19 43 51 -21 17.8 0.984 -35.020 87.148 Set
...
s 0 is Pluto
s 1 is 18
s 2 is 6
s 3 is 40
s 4 is -17
s 5 is 44.9
s 6 is 32.485
s 7 is -52.833
s 8 is 108.052
s 9 is Set
Pluto 18 6 40 -17 44.9 32.485 -52.833 108.052 Set

C:\MinGW\source>

Fully quoted.

Nobody is giving you a proper solution to your original problem
representation, only because you won't take that solution, which is
the real solution.

Instead, whore attenition monger, you have to make a complete idiot out of
yourself and post sub problems, not even relative to the original problem,
blown way out of proportion to the nth degree, all because you can't find your
way around the documentation like everybody else.

Your entire problem is solved with a single 20 character regular expression,
but your a nut job that needs a book read to him before bedtime.

sln
 
L

Larry Gates

Open another file for writing and write the modified lines to that
file. When you are all done, you can rename the new file to the name of
the old file, thereby deleting the old file, or you can just use the
new file instead.

I sure made a mess of this:

use strict;
use warnings;


my $filename = 'eph6.txt';
my $filename2 = 'outfile1.txt';
my $filename3 = 'temp.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
open(my $gh, '+>', $filename2) or die "cannot open $filename2: $!";
open(my $hh, '+>', $filename3) or die "cannot open $filename3: $!";

while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}
close($fh);

seek($gh,0,0);

while (my $line = <$gh>) {
my @s = split /\s+/, $line;

$s[1] =~ s/h//;
$s[2] =~ s/m//;
$s[3] =~ s/s//;
$s[5] =~ s/'//;
for my $i (0..9) {
print STDOUT "s$i is $s[$i]\n";
}

$line = join(' ',@s);
print "$line\n";
print $hh "$line\n";
}
close($fh);
close($gh);
close($hh);

#rename $filename3, $filename2;

# perl reg10.pl

I think I see what Jue was calling a noop and uri guttman was scratching
his head about. Do I need a temp file at all for this? I thought I was
reading and writing from $gh but apparently not. I wonder how I ever got
output at all.??
--
larry gates

And besides, if Perl really takes off in the Windows space, I think the
rest of us would just as soon have a double-agent within ActiveState. :)
-- Larry Wall in <[email protected]>
 
T

Tad J McClellan

You have not answered the question.

Why do you think you need those lines in your program?

I'm pleased to roll out
my first output that I can call a solution:

while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}
close($fh);


Delete those lines and compare the output to when those lines
are included.

There will be no differences.

ie. those lines do not do anything useful, so remove them.
 
S

sln

Open another file for writing and write the modified lines to that
file. When you are all done, you can rename the new file to the name of
the old file, thereby deleting the old file, or you can just use the
new file instead.

I sure made a mess of this:

use strict;
use warnings;


my $filename = 'eph6.txt';
my $filename2 = 'outfile1.txt';
my $filename3 = 'temp.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
open(my $gh, '+>', $filename2) or die "cannot open $filename2: $!";
open(my $hh, '+>', $filename3) or die "cannot open $filename3: $!";

while (my $line = <$fh>) {
$line =~ s/\t/ /g;
$line =~ s/ER/ /g;
$line =~ s/°/ /g;
}
close($fh);

seek($gh,0,0);

while (my $line = <$gh>) {
my @s = split /\s+/, $line;

$s[1] =~ s/h//;
$s[2] =~ s/m//;
$s[3] =~ s/s//;
$s[5] =~ s/'//;
for my $i (0..9) {
print STDOUT "s$i is $s[$i]\n";
}

$line = join(' ',@s);
print "$line\n";
print $hh "$line\n";
}
close($fh);
close($gh);
close($hh);

#rename $filename3, $filename2;

# perl reg10.pl

I think I see what Jue was calling a noop and uri guttman was scratching
his head about. Do I need a temp file at all for this? I thought I was
reading and writing from $gh but apparently not. I wonder how I ever got
output at all.??

I'm sure your mother was shocked too...

sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top