opening a file

Discussion in 'Perl Misc' started by George, Jan 8, 2009.

  1. George

    George Guest

    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 and send it to STDOUT. This may
    come across as a contemptable FAQ to some. I've just looked at all the
    entries with the string 'file' beginning them in the index of the camel
    book, and it's huge and ambiguous to a guy who is not in the native perl
    environment, ie, a server, but simply needs to get a little io on windows.

    This is what I have so far:

    use strict;
    use warnings;


    my $divider= "%\n" # a percentage sign and a newline


    # perl scraper1.pl

    In fortran, the snippet would be:

    open(unit=50,file='ehp3.txt',form='formatted')
    do
    read(50,*,iostat=eof) line
    if (eof /= 0) exit
    write(*,*) trim(line)
    end do
    close(unit=50)

    Thanks for your comment.
    --
    George

    It's going to be the year of the sharp elbow and the quick tongue.
    George W. Bush

    Picture of the Day http://apod.nasa.gov/apod/
     
    George, Jan 8, 2009
    #1
    1. Advertising

  2. George <> 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

    >This may
    >come across as a contemptable FAQ to some. I've just looked at all the
    >entries with the string 'file' beginning them in the index of the camel
    >book, and it's huge and ambiguous to a guy who is not in the native perl
    >environment, ie, a server,


    There is no relation between Perl and servers.

    >but simply needs to get a little io on windows.
    >
    >This is what I have so far:
    >
    > use strict;
    > use warnings;
    >
    >
    >my $divider= "%\n" # a percentage sign and a newline
    >
    >
    ># perl scraper1.pl
    >
    >In fortran, the snippet would be:


    I do not speak Fortran, therefore I cannot translate
    Maybe you could just describe what your program is meant to achieve, aka
    a spec?

    jue
     
    Jürgen Exner, Jan 8, 2009
    #2
    1. Advertising

  3. George

    Mirco Wahab Guest

    George wrote:
    > So it is that I need to open a text file and send it to STDOUT. This may
    > come across as a contemptable FAQ to some. I've just looked at all the
    > entries with the string 'file' beginning them in the index of the camel
    > book, and it's huge and ambiguous to a guy who is not in the native perl
    > environment, ie, a server, but simply needs to get a little io on windows.
    >
    > This is what I have so far:
    > use strict;
    > use warnings;
    > my $divider= "%\n" # a percentage sign and a newline


    Whats meant by 'divider'. Is this the
    'input record seperator'. Your fortran
    example doesn't give a hint.

    > In fortran, the snippet would be:
    > open(unit=50,file='ehp3.txt',form='formatted')
    > do
    > read(50,*,iostat=eof) line
    > if (eof /= 0) exit
    > write(*,*) trim(line)
    > end do
    > close(unit=50)


    This is the closest I could get (under strict):

    open(50, '<ehp3.txt');
    DO:
    { my $line = readline(*50);
    if(eof != 0) { exit }
    print $line; # no 'write' here
    redo DO } # no 'end' possible
    close(50)


    This is, of course, just for fun and not
    really useful ;-) But it works.

    The real "perlish" approach to this
    problem is already given in the other
    responses to your question.

    Regards

    M.
     
    Mirco Wahab, Jan 8, 2009
    #3
  4. George 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 and send it to STDOUT. This may
    > come across as a contemptable FAQ to some. I've just looked at all the
    > entries with the string 'file' beginning them in the index of the camel
    > book, and it's huge and ambiguous to a guy who is not in the native perl
    > environment, ie, a server, but simply needs to get a little io on windows.
    >
    > This is what I have so far:
    >
    > use strict;
    > use warnings;
    >
    >
    > my $divider= "%\n" # a percentage sign and a newline
    >
    >
    > # perl scraper1.pl
    >
    > In fortran, the snippet would be:
    >
    > open(unit=50,file='ehp3.txt',form='formatted')
    > do
    > read(50,*,iostat=eof) line
    > if (eof /= 0) exit
    > write(*,*) trim(line)
    > end do
    > close(unit=50)
    >
    > Thanks for your comment.


    This is all that you need

    #The manuals don't really say it but files have their own data type.
    #This line creates a variable called file or exits saying that it can't open
    #the file with a possible explanation (the $! variable).
    open( FILE, "ehp4.txt") || die "Could not open ehp3.txt $!";

    #read out one line at a time until the file ends
    while( <FILE> ){
    print $_; #If you don't specify a file it goes to STDOUT (STandard OUTput)
    #The $_ variable is created by the while statement
    }

    close(FILE); #This might not really be needed


    On Unix type systems (Linux and BSD are not called Unix for legal reasons)
    all programs are told about a set of files called stdin, stdout and stderr.
    These act as the command line input / output (the last one is to prevent
    error messages from being confused with normal output).

    If you have a choice try switching over to Linux (FreeBSD later perhaps) and
    do your programming their. Most of the perl concepts come from that sort
    of environment.
    I would describe my system (running Fedora Linux version 10) as a born as
    PC and perl fits in very well.
    The GNU Compiler Collection also has optional FORTRAN support.

    ------------------------------------------------------------------------
    Quote of the login:
    Given a choice between grief and nothing, I'd choose grief.
    -- William Faulkner
     
    Ryan McCoskrie, Jan 8, 2009
    #4
  5. George

    George Guest

    On Wed, 07 Jan 2009 19:05:01 -0800, Jürgen Exner wrote:

    > George <> 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.

    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.


    >
    >>This may
    >>come across as a contemptable FAQ to some. I've just looked at all the
    >>entries with the string 'file' beginning them in the index of the camel
    >>book, and it's huge and ambiguous to a guy who is not in the native perl
    >>environment, ie, a server,

    >
    > There is no relation between Perl and servers.


    What language do sysadmins use?

    >
    >>but simply needs to get a little io on windows.
    >>
    >>This is what I have so far:
    >>
    >> use strict;
    >> use warnings;
    >>
    >>
    >>my $divider= "%\n" # a percentage sign and a newline
    >>
    >>
    >># perl scraper1.pl
    >>
    >>In fortran, the snippet would be:

    >
    > I do not speak Fortran, therefore I cannot translate
    > Maybe you could just describe what your program is meant to achieve, aka
    > a spec?
    >
    > jue


    Thanks for your response, jü.

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

    %%
    "And I don't like doing silly things (except on purpose)."
    -- Larry Wall in <>
    %%
    : And it goes against the grain of building small tools.
    Innocent, Your Honor. Perl users build small tools all day long.
    -- Larry Wall in <>
    %%
    /* 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.
    --
    George

    America is a Nation with a mission - and that mission comes from our most
    basic beliefs. We have no desire to dominate, no ambitions of empire. Our
    aim is a democratic peace - a peace founded upon the dignity and rights of
    every man and woman.
    George W. Bush

    Picture of the Day http://apod.nasa.gov/apod/
     
    George, Jan 8, 2009
    #5
  6. Ryan McCoskrie wrote:

    I hope you don't mind, I added a few comments to your advice.

    > George 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 and send it to STDOUT. This may
    >> come across as a contemptable FAQ to some. I've just looked at all the
    >> entries with the string 'file' beginning them in the index of the camel
    >> book, and it's huge and ambiguous to a guy who is not in the native perl
    >> environment, ie, a server, but simply needs to get a little io on windows.
    >>
    >> This is what I have so far:
    >>
    >> use strict;
    >> use warnings;


    always a good start.

    >>
    >>
    >> my $divider= "%\n" # a percentage sign and a newline
    >>
    >>
    >> # perl scraper1.pl
    >>
    >> In fortran, the snippet would be:
    >>
    >> open(unit=50,file='ehp3.txt',form='formatted')
    >> do
    >> read(50,*,iostat=eof) line
    >> if (eof /= 0) exit
    >> write(*,*) trim(line)
    >> end do
    >> close(unit=50)


    You read from ehp3.txt and write to standard output?
    You might find Perl's -p option useful.

    perl -p -e "#do something with one line" ehp3.txt

    >>
    >> Thanks for your comment.

    >
    > This is all that you need
    >
    > #The manuals don't really say it but files have their own data type.
    > #This line creates a variable called file or exits saying that it can't open
    > #the file with a possible explanation (the $! variable).
    > open( FILE, "ehp4.txt") || die "Could not open ehp3.txt $!";


    There is a bug in that line. This is why I prefer to write
    my $filename = 'ehp4.txt';
    open my $handle, '<', $filename
    or die "unable to open '$filename' because $!";

    >
    > #read out one line at a time until the file ends
    > while( <FILE> ){
    > print $_; #If you don't specify a file it goes to STDOUT (STandard OUTput)


    "print;" is the same as "print $_;" and shorter and more idiomatic.

    > #The $_ variable is created by the while statement
    > }
    >
    > close(FILE); #This might not really be needed
    >
    >


    I think the OP might be looking for something like

    perl -p -e "s/%%/%\n/" ehp3.txt

    which is roughly equivalent to
    -------------------8<------------------------------
    #!/usr/bin/perl
    use strict;
    use warnings;

    my $filename = 'ehp3.txt';
    open my $handle, '<', $filename
    or die "unable to open '$filename' because $!";
    while (<$handle>) {
    s/%%/%\n/;
    print;
    }
    close $handle;
    -------------------8<------------------------------
    Untested - caveat emptor.

    --
    RGB
     
    RedGrittyBrick, Jan 8, 2009
    #6
  7. George

    smallpond Guest

    On Jan 8, 4:45 am, George <> wrote:
    > On Wed, 07 Jan 2009 19:05:01 -0800, Jürgen Exner wrote:
    > > George <> 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.
    >
    > 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.
    >
    >


    filehandles are a feature of the OS and filesystems across all
    languages.
    What do you think logical unit means in FORTRAN?

    Trying to get started in a language by reading detailed command
    descriptions is like learning to paint a forest by asking a botanist
    about
    the cell structure of trees. Get a copy of "Learning Perl".
     
    smallpond, Jan 8, 2009
    #7
  8. George

    cartercc Guest

    On Jan 7, 9:50 pm, George <> 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.


    Easy, easy, easy ...

    open INFILE, "<nameoffile.txt";
    while (<INFILE>) {print;}
    close INFILE;

    This creates the handle INFILE for the named file in your current
    directory and cycles through it line by line, printing each line. To
    write the file to an out handle, do this:

    open INFILE, "<nameoffile.txt";
    open OUTFILE, ">nameofnewfile.txt";
    while (<INFILE>) { print OUTFILE;}
    close OUTFILE;
    close INFILE;

    CC.
     
    cartercc, Jan 8, 2009
    #8
  9. George <> wrote:
    >On Wed, 07 Jan 2009 19:05:01 -0800, Jürgen Exner wrote:
    >> George <> 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 <>
    >%%
    >: And it goes against the grain of building small tools.
    >Innocent, Your Honor. Perl users build small tools all day long.
    > -- Larry Wall in <>
    >%%
    >/* 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.
     
    Jürgen Exner, Jan 8, 2009
    #9
  10. cartercc <> wrote:
    >On Jan 7, 9:50 pm, George <> 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.

    >
    >Easy, easy, easy ...
    >
    >open INFILE, "<nameoffile.txt";


    Most people would suggest to use the three-argument form of open:
    open INFILE, '<', 'nameoffile.txt';

    Almost all people would strongly suggest to test for failure:
    open INFILE, '<', 'nameoffile.txt' or
    die "Cannot open nameoffile.txt because $!\n";

    jue
     
    Jürgen Exner, Jan 8, 2009
    #10
  11. George

    Dr.Ruud Guest

    George 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 and send it to STDOUT.


    You don't, because you can let your OS do that for you:

    ./filter.pl < input-file > output-file

    In your filter.pl you put something like:

    #/usr/bin/perl
    use strict;
    use warnings;

    while ( <> ) {
    substr $_, 50, -1, "..."
    if length > 50;
    }
    __END__

    --
    Ruud
     
    Dr.Ruud, Jan 9, 2009
    #11
  12. George

    George Guest

    On Thu, 08 Jan 2009 21:25:17 +1300, Ryan McCoskrie wrote:

    > George wrote:


    >
    > This is all that you need
    >
    > #The manuals don't really say it but files have their own data type.
    > #This line creates a variable called file or exits saying that it can't open
    > #the file with a possible explanation (the $! variable).
    > open( FILE, "ehp4.txt") || die "Could not open ehp3.txt $!";
    >
    > #read out one line at a time until the file ends
    > while( <FILE> ){
    > print $_; #If you don't specify a file it goes to STDOUT (STandard OUTput)
    > #The $_ variable is created by the while statement


    I didn't know that.


    > }
    >
    > close(FILE); #This might not really be needed
    >
    >
    > On Unix type systems (Linux and BSD are not called Unix for legal reasons)
    > all programs are told about a set of files called stdin, stdout and stderr.
    > These act as the command line input / output (the last one is to prevent
    > error messages from being confused with normal output).
    >
    > If you have a choice try switching over to Linux (FreeBSD later perhaps) and
    > do your programming their. Most of the perl concepts come from that sort
    > of environment.
    > I would describe my system (running Fedora Linux version 10) as a born as
    > PC and perl fits in very well.
    > The GNU Compiler Collection also has optional FORTRAN support.


    Thanks for your response, Ryan. What do you mean by this? I'm well
    acquainted with the gfortran and g95 folks; indeed, I use their compilers.
    Do you mean support that doesn't take the form of a compiler?

    I ran the script in the same directory as the target, and I missed:

    C:\MinGW\source>perl faulk1.pl
    Could not open ehp3.txt No such file or directory at faulk1.pl line 4.

    C:\MinGW\source>dir
    Volume in drive C has no label.
    Volume Serial Number is 942A-AD55

    Directory of C:\MinGW\source


    01/04/2009 07:50 PM 566 eph3.txt
    01/05/2009 01:57 PM 1,576 eph4.f03
    01/06/2009 10:51 PM 532 eph4.txt
    01/05/2009 02:11 PM 1,567 eph5.f03
    01/06/2009 10:56 PM 730 eph6.f03
    01/06/2009 11:15 PM 852 eph7.f03
    01/06/2009 04:43 PM 465 eps1.f03
    11/27/2003 02:31 PM 13,333 f2kcli.f90
    12/14/2008 01:28 PM 2,662 f2kcli.mod
    01/09/2009 06:18 PM 538 faulk1.pl
    01/30/2000 03:19 PM 1,276 fax.h


    C:\MinGW\source>

    Any ideas?
    --
    George

    Bring them on.
    George W. Bush

    Picture of the Day http://apod.nasa.gov/apod/
     
    George, Jan 10, 2009
    #12
  13. George

    George Guest

    On Thu, 08 Jan 2009 09:04:21 +0100, Mirco Wahab wrote:

    > open(50, '<ehp3.txt');
    > DO:
    > { my $line = readline(*50);
    > if(eof != 0) { exit }
    > print $line; # no 'write' here
    > redo DO } # no 'end' possible
    > close(50)


    Shoot, I get nothing for output here. I added asymbol to yours, but I
    still get nothing. I have to believe that USER ERROR is rearing its head
    and laughing at me.

    open(50, '<ehp3.txt>');
    DO:
    { my $line = readline(*50);
    if(eof != 0) { exit }
    print $line; # no 'write' here
    redo DO } # no 'end' possible
    close(50)

    # perl faulk3.pl


    C:\MinGW\source>perl faulk1.pl
    Could not open ehp3.txt No such file or directory at faulk1.pl line 4.

    11/30/2008 01:54 AM 17 dump.bat
    12/13/2008 01:13 PM 0 e
    12/13/2008 01:07 PM 455,220 e.exe
    02/06/2000 11:00 AM 18,733 encode.c
    12/06/2008 08:18 PM 18,763 encode1.c
    12/31/2008 07:14 PM 79 eph1.f03
    12/31/2008 06:32 PM 1,098 eph1.txt
    01/02/2009 12:43 AM 672 eph2.f03
    12/31/2008 06:52 PM 217 eph2.txt
    01/03/2009 07:23 PM 1,807 eph3.f03
    01/04/2009 07:50 PM 566 eph3.txt
    01/05/2009 01:57 PM 1,576 eph4.f03
    01/06/2009 10:51 PM 532 eph4.txt
    01/05/2009 02:11 PM 1,567 eph5.f03
    01/06/2009 10:56 PM 730 eph6.f03
    01/06/2009 11:15 PM 852 eph7.f03
    01/06/2009 04:43 PM 465 eps1.f03
    11/27/2003 02:31 PM 13,333 f2kcli.f90
    12/14/2008 01:28 PM 2,662 f2kcli.mod
    01/09/2009 06:18 PM 538 faulk1.pl
    01/30/2000 03:19 PM 1,276 fax.h
    12/12/2008 08:51 PM 2,719 fin1.txt
    12/30/2008 10:41 PM 7,683 frank1.txt
    01/07/2009 02:24 AM 6,879 frank2.txt

    C:\MinGW\source>perl faulk2.pl
    unable to open 'ehp3.txt' because No such file or directory at faulk2.pl
    line 6.


    C:\MinGW\source>perl faulk2.pl
    unable to open 'ehp3.txt' because No such file or directory at faulk2.pl
    line 6.


    C:\MinGW\source>perl faulk3.pl

    C:\MinGW\source>perl faulk3.pl

    C:\MinGW\source>

    ??:-(\
    --
    George

    The United States and our allies are determined: we refuse to live in the
    shadow of this ultimate danger.
    George W. Bush

    Picture of the Day http://apod.nasa.gov/apod/
     
    George, Jan 10, 2009
    #13
  14. George

    George Guest

    On Fri, 09 Jan 2009 12:09:43 +0100, Dr.Ruud wrote:

    > ./filter.pl



    C:\MinGW\source> ./filter.pl
    '.' is not recognized as an internal or external command,
    operable program or batch file.

    C:\MinGW\source>
    --
    George

    Freedom itself was attacked this morning by a faceless coward, and freedom
    will be defended.
    George W. Bush

    Picture of the Day http://apod.nasa.gov/apod/
     
    George, Jan 10, 2009
    #14
  15. George

    George Guest

    On Wed, 7 Jan 2009 20:57:31 -0800, Keith Keller wrote:

    > perldoc -q 'entire file'


    Keith,

    I seem completely befuddled by bonehead stuff here. An example is that dos
    gives me nothing for perldoc; what's more, I find options on the net that
    don't seem to be as simple as this task must be.

    http://perldoc.perl.org/File/Find.html

    OTOH, maybe perl has completely different notions. I'm quite clueless
    here. What can I say: I'm George.
    --
    George

    You can fool some of the people all the time, and those are the ones you
    want to concentrate on.
    George W. Bush

    Picture of the Day http://apod.nasa.gov/apod/
     
    George, Jan 10, 2009
    #15
  16. George <> wrote:
    >Shoot, I get nothing for output here. I added asymbol to yours, but I
    >still get nothing. I have to believe that USER ERROR is rearing its head
    >and laughing at me.
    >
    > open(50, '<ehp3.txt>');
    > DO:
    > { my $line = readline(*50);
    > if(eof != 0) { exit }
    > print $line; # no 'write' here
    > redo DO } # no 'end' possible
    > close(50)


    Ouch! This hurts! I suppose some people manage to program in Fortran no
    matter what programming language they are using.

    >Could not open ehp3.txt No such file or directory at faulk1.pl line 4.

    ^^^^^^^^
    [...]
    >01/04/2009 07:50 PM 566 eph3.txt

    ^^^^^^^^

    Dah!

    jue
     
    Jürgen Exner, Jan 10, 2009
    #16
  17. George <> wrote:
    >On Fri, 09 Jan 2009 12:09:43 +0100, Dr.Ruud wrote:
    >
    >> ./filter.pl

    >
    >
    >C:\MinGW\source> ./filter.pl
    >'.' is not recognized as an internal or external command,
    >operable program or batch file.


    You might want to learn how to use your OS/command shell.

    .\filter.pl

    jue
     
    Jürgen Exner, Jan 10, 2009
    #17
  18. George

    George Guest

    On Fri, 09 Jan 2009 18:24:38 -0800, Jürgen Exner wrote:

    > George <> wrote:
    >>Shoot, I get nothing for output here. I added asymbol to yours, but I
    >>still get nothing. I have to believe that USER ERROR is rearing its head
    >>and laughing at me.
    >>
    >> open(50, '<ehp3.txt>');
    >> DO:
    >> { my $line = readline(*50);
    >> if(eof != 0) { exit }
    >> print $line; # no 'write' here
    >> redo DO } # no 'end' possible
    >> close(50)

    >
    > Ouch! This hurts! I suppose some people manage to program in Fortran no
    > matter what programming language they are using.


    That's what they say about fortran, is that you do it in any syntax. If
    you don't eventually "give in" to the ethos of a scripting language, you
    miss the better part of it. Wir meinen damit daß das abwertend ist,
    fortran sonstwo zu üben.

    For example, there's one line in a perl script. Mirko's script does well
    to entertain my previous notions here. In particular, the unit number in
    fortran looks like it works as a file handle for perl. Am I correct that
    50 is a perfectly kosher fh here.

    That the read avails itself of fifty with one level of indirection is
    something that one might expect of a child of C.

    >
    >>Could not open ehp3.txt No such file or directory at faulk1.pl line 4.

    > ^^^^^^^^
    > [...]
    >>01/04/2009 07:50 PM 566 eph3.txt

    > ^^^^^^^^
    >
    > Dah!
    >
    > jue


    Ich bin ahnungslos:

    C:\MinGW\source>perldoc -q 'entire file'
    No documentation for perl FAQ keyword `'entire' found

    C:\MinGW\source>perl faulk3.pl

    C:\MinGW\source>type eph3.txt
    ! yesterday
    # another comment

    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
    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
    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
    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
    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

    C:\MinGW\source>type faulk3.pl
    open(50, '<eph3.txt>');
    DO:
    { my $line = readline(*50);
    if(eof != 0) { exit }
    print $line; # no 'write' here
    redo DO } # no 'end' possible
    close(50)

    # perl faulk3.pl
    C:\MinGW\source>


    --
    George

    I believe the most solemn duty of the American president is to protect the
    American people. If America shows uncertainty and weakness in this decade,
    the world will drift toward tragedy. This will not happen on my watch.
    George W. Bush

    Picture of the Day http://apod.nasa.gov/apod/
     
    George, Jan 10, 2009
    #18
  19. George

    Tim Greer Guest

    George wrote:

    > ran the script in the same directory as the target, and I missed:
    >
    > C:\MinGW\source>perl faulk1.pl
    > Could not open ehp3.txt No such file or directory at faulk1.pl line 4.
    >
    > C:\MinGW\source>dir
    > Volume in drive C has no label.
    > Volume Serial Number is 942A-AD55
    >
    > Directory of C:\MinGW\source
    >
    >
    > 01/04/2009  07:50 PM               566 eph3.txt


    >
    >
    > C:\MinGW\source>
    >
    > Any ideas?


    yes, eph.txt and ehp.txt are not the same file name.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 10, 2009
    #19
  20. On 2009-01-10 03:11, George <> wrote:
    > On Fri, 09 Jan 2009 18:24:38 -0800, Jürgen Exner wrote:
    >> George <> wrote:
    >>>Shoot, I get nothing for output here. I added asymbol to yours, but I
    >>>still get nothing. I have to believe that USER ERROR is rearing its head
    >>>and laughing at me.
    >>>
    >>> open(50, '<ehp3.txt>');
    >>> DO:
    >>> { my $line = readline(*50);
    >>> if(eof != 0) { exit }
    >>> print $line; # no 'write' here
    >>> redo DO } # no 'end' possible
    >>> close(50)

    >>
    >> Ouch! This hurts!


    I'm sure Mirco meant that as a joke.

    >> I suppose some people manage to program in Fortran no
    >> matter what programming language they are using.

    >
    > That's what they say about fortran, is that you do it in any syntax. If
    > you don't eventually "give in" to the ethos of a scripting language, you
    > miss the better part of it.


    Right. See below.

    > Wir meinen damit daß das abwertend ist, fortran sonstwo zu üben.


    I'm not sure whether this sentence means what you think it means ;-)


    > For example, there's one line in a perl script. Mirko's script does well
    > to entertain my previous notions here. In particular, the unit number in
    > fortran looks like it works as a file handle for perl. Am I correct that
    > 50 is a perfectly kosher fh here.


    Not quite. A slight hint that it isn't perfectly kosher is that a "*" is
    needed in the argument list to readline to distinguish it from the
    number 50.

    Use lexical file handles:

    open(my $fh, '<ehp3.txt>');
    DO:
    { my $line = readline($fh);
    if(eof != 0) { exit }
    print $line;
    redo DO }
    close($fh)

    Use the three argument form of open:

    open(my $fh, '<', 'ehp3.txt>');
    DO:
    { my $line = readline($fh);
    if(eof != 0) { exit }
    print $line;
    redo DO }
    close($fh)

    (which btw, makes it clear that the file name is wrong. You don't want
    to open 'ehp3.txt>', but 'ehp3.txt', or rather 'eph3.txt')

    Use proper loops instead using redo to jump back to the
    beginning of a block.

    open(my $fh, '<', 'eph3.txt');
    while (my $line = readline($fh)) {
    print $line;
    }
    close($fh)

    Aquaint yourself with the operator form of readline.

    open(my $fh, '<', 'eph3.txt');
    while (my $line = <$fh>) {
    print $line;
    }
    close($fh)

    And you might want to use the $_ variable:

    open(my $fh, '<', 'eph3.txt');
    while (<$fh>) {
    print $_;
    }
    close($fh)

    In short: Write Perl, not Perl-disguised-as-Fortran.

    And of course you should check for errors:

    open(my $fh, '<', 'eph3.txt') or die "cannot open eph3.txt: $!";
    while (<$fh>) {
    print $_;
    }
    close($fh)

    And you should use variables if you use the same string more than once:

    my $filename = 'eph3.txt';
    open(my $fh, '<', $filename) or die "cannot open $filename: $!";
    while (<$fh>) {
    print $_;
    }
    close($fh)

    (this is especially important for error messages. If you misspell a
    filename in the call to open, but not in the error message, the error
    message is extremely confusing)

    hp
     
    Peter J. Holzer, Jan 10, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    10
    Views:
    4,960
    Old Wolf
    Feb 25, 2004
  2. Replies:
    7
    Views:
    500
  3. amacch95

    opening media file in asp file

    amacch95, Apr 17, 2008, in forum: .NET
    Replies:
    0
    Views:
    340
    amacch95
    Apr 17, 2008
  4. fniles
    Replies:
    0
    Views:
    277
    fniles
    Apr 26, 2009
  5. John Smith
    Replies:
    2
    Views:
    293
    Brian McCauley
    Dec 1, 2003
Loading...

Share This Page