Re: opening a file



George <george@xxxxxxxxxxxxxxx> wrote:
On Wed, 07 Jan 2009 19:05:01 -0800, Jürgen Exner wrote:
George <george@xxxxxxxxxxxxxxx> wrote:
I thought I would use perl instead of fortran to parse a text file, what
with the new m// s/// capabilities at my fingertips.

So it is that I need to open a text file

perldoc -f open

and send it to STDOUT.

perldoc -f print

Both of these turn up a lot of information, most of which is overkill for
this purpose. Furthermore, perl's features are simply confusing for me in
that there are so many, and I only need a little text file that's already
on my machine.

I strongly suggest you get an introductory book about Perl. Learning the
concepts of programming language by looking at individual commands is
doomed to fail.

Even "Programming Perl", although not a tutorial, explains file handles
and open() on page 12 and 13 already and reading from a file is
introduced informally together with "while"on page 25 and used in
exactly the way you are looking for on page 27.

The standard Perl idiom for reading and looping through a file is

open(MyFile, '<', $MyFileName) or
die "Cannot open $MyFileName for reading because of $!\n";
while (my $line = <MyFile>) {
#process $line here
}
close MyFile or die "Cannot close file because of $!\n";

It brings up the notion of filehandles, which is fine, but it is something
I only know from C++ and in an MS development environment. I understand
how to open files in C.

Well, it's very similar in Perl. You provide a file handle, the mode,
and the name of the file.

There is no relation between Perl and servers.

What language do sysadmins use?

Whatever fits the bill. But sysadmin and servers only related by
coincidence. While it is true that many servers are maintained by
sysadmins, there are just as many servers not maintained by sysadmins
and just as many sysadmins, who couldn't care less about servers.

Nominally, I want to delimit Larry Wall's sigs in a manner that will work
for my next identity.

What? Are you a nym shifter?

They look like:

%%
"And I don't like doing silly things (except on purpose)."
-- Larry Wall in <1992Jul3.191825.14435@xxxxxxxxxxx>
%%
: And it goes against the grain of building small tools.
Innocent, Your Honor. Perl users build small tools all day long.
-- Larry Wall in <1992Aug26.184221.29627@xxxxxxxxxxx>
%%
/* And you'll never guess what the dog had */
/* in its mouth... */
-- Larry Wall in stab.c from the perl source code
%%

, and I want to take the %% and replace it with $divider.

Otherwise I want to condition the set that this fortran program outputs. I
have no idea what to do with some of these characters.


module arjen2

type solar_system
type(solar_object), dimension(:), pointer :: object
end type

type solar_object
character(len=20) :: name
integer :: ascent_hour, ascent_minute, ascent_second
integer :: dec_degrees
real :: dec_mins, distance, azimuth, altitude
logical :: ER, UP

end type

end module arjen2

! default precision


integer, parameter:: max = 1023

integer:: ln, istat

character(len=max):: line

open(unit=50,file='eph4.txt')

do
Read(50,'(A)',IOSTAT=istat) line
ln = Len_Trim(line)
If (istat /= 0) Exit
call parser(line, ln)

write(*,*) trim(line)
end do
contains
subroutine parser(line, ln)
use arjen2
integer::ln
character(len=ln):: line

print *, line, ln



end subroutine

endprogram
! g95 eph7.f03 -o r.exe

C:\MinGW\source>g95 eph7.f03 -o r.exe

C:\MinGW\source>r
Sun 18h 41m 55s -23? 5.4' 0.983 10.215 52.155 Up 48
Sun 18h 41m 55s -23? 5.4' 0.983 10.215 52.155 Up
Mercury 20h 2m 16s -22? 12.5' 1.102 22.537 37.668 Up
52
Mercury 20h 2m 16s -22? 12.5' 1.102 22.537 37.668 Up
Venus 21h 55m 33s -14? 16.3' 0.795 39.872 11.703 Up 51
Venus 21h 55m 33s -14? 16.3' 0.795 39.872 11.703 Up
Moon 21h 17m 19s -15? 2.4' 62.4 ER 36.796 22.871 Up 51
Moon 21h 17m 19s -15? 2.4' 62.4 ER 36.796 22.871 Up
Mars 18h 11m 59s -24? 6.1' 2.431 4.552 56.184 Up 48
Mars 18h 11m 59s -24? 6.1' 2.431 4.552 56.184 Up
Jupiter 20h 3m 35s -20? 49.4' 6.034 23.867 38.203 Up
52
Jupiter 20h 3m 35s -20? 49.4' 6.034 23.867 38.203 Up
Saturn 11h 32m 59s +5? 8.6' 9.018 -47.333 157.471 Set 53
Saturn 11h 32m 59s +5? 8.6' 9.018 -47.333 157.471 Set
Uranus 23h 21m 30s -4? 57.9' 20.421 48.328 -18.527 Up 53
Uranus 23h 21m 30s -4? 57.9' 20.421 48.328 -18.527 Up
Neptune 21h 39m 30s -14? 22.8' 30.748 38.963 16.599 Up
54
Neptune 21h 39m 30s -14? 22.8' 30.748 38.963 16.599 Up
Pluto 18h 4m 34s -17? 44.5' 32.543 7.443 62.142 Up 50
Pluto 18h 4m 34s -17? 44.5' 32.543 7.443 62.142 Up

This is how the data fits with the declarations:

Pluto : name
18 ascent_hour
4 ascent_minute
34 ascent_second
-17 dec_degrees
44.5 dec_mins
32.543 distance
0 ER
7.443 altitude
62.142 azimuth
1 UP

Sorry about the longish post, but you asked.
.



Relevant Pages

  • Re: Net::DNS
    ... function because you still use $rr within the foreach, ... > So I can find the all these object methods in the perl docs listed below? ... > the return value of 'grep'. ... > perldoc perlboot ...
    (perl.beginners)
  • Re: Posting Guidelines changes (was Re: variable interpolation of refs to anonymous subroutines)
    ... Experts at Perl or experts at posting to newsgroups? ... use warnings; # see perldoc warnings ...
    (comp.lang.perl.misc)
  • Re: Multitasking
    ... Just a comment for the archives, and to any other beginner to the Perl ... When you look at the perldoc for select, perldoc -f select, you ... select FILEHANDLE ... I just meant avoid using the "Low-level socket functions" ...
    (perl.beginners)
  • Re: DBD::Oracle - execute_array core dumps intermittently [SOLVED]
    ... difference in the servers. ... If you have problems you may need to rebuild perl with threading enabled. ... I never compile with threads, since we have not found a need for them, yet. ... I seem to remember that on Linux I could build code asking for thread-safe and mix it with libraries which were not built this way and the linker did not complain. ...
    (perl.dbi.users)
  • Re: Teach me how to fish, regexp
    ... Thanks for your response on this thread: ... On the other hand, maybe "older" perl is ... > everything up in perldoc and man pages. ... > pattern so that a quantifier can apply to it. ...
    (comp.lang.perl.misc)