Opening a text file and editing its contents

Discussion in 'Perl Misc' started by toomanyjoes@mail.utexas.edu, Jan 14, 2005.

  1. Guest

    I'm still having problems opening a file. I looked at the docs and this
    is what I came up with.


    open (FILE, "> C:\notes\gen.txt") || die ("error $!\n"); #See note 1


    while (<FILE>) { #See note 2


    s/\<[NR][a\d]\>/ /g #note 3


    if (^\w\D\D\s\d+:\d+) #note 4


    Note1: Although in all the examples I've looked at the actual file
    reference is always very vague and I have never seen a full path with
    the files extension in the quotes like I have done here.


    Note2: Here I want to loop through the file, is this correct?


    Note3: Here I want to find every instance (In the File) of a "<Ra> or a
    <N1>(2,3,etc.)" and replace it with nothing (basically remove it)


    Note4: Here I'm looking for a specific arrangement of characters at the
    beginning of the line it can be any character followed by 2 letters
    followed by a space followed by any number of digits separated by a
    colon. (ex. 1RA 12321:123214 would match as well as ADD 4:2) if a match
    occurs I want to remove the colon and separate these items with a
    delimiter. For Example (ex. 1RA/ 12321/123214)


    And thats as far as I've been able to get because I get an error
    opening the file. It looks to me like I've done it by the book. But I
    must be missing something. I'd appreciate any comments about my code.
    Thanks,
    Joe
     
    , Jan 14, 2005
    #1
    1. Advertising

  2. wrote:

    > I'm still having problems opening a file. I looked at the docs and this
    > is what I came up with.


    Posting the same message multiple times is rude. Please don't do that.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jan 14, 2005
    #2
    1. Advertising

  3. In article <> posted
    on Fri, 14 Jan 2005 11:28:23 -0800, toomanyjoes wrote:

    > I'm still having problems opening a file. I looked at the docs and this
    > is what I came up with.
    >
    >
    > open (FILE, "> C:\notes\gen.txt") || die ("error $!\n"); #See note 1


    Note that \ is special here. In this case, \n translates to 'newline' and
    and \g (I think) translates to the ASCII bell character. You would have
    noticed this had you tried to print the file name.

    Also, I do prefer using lexical filehandles and the 3 argument form of
    open:

    my $fn = 'c:/notes/gen.txt';

    open my $file, '>', $fn
    or die "Cannot open $fn: $!";

    > while (<FILE>) { #See note 2


    Your posting style makes it very cumbersome to answer your other questions.

    Sinan.

    --
    A. Sinan Unur
    d -- remove invalid and
    reverse each component for email address.
     
    A. Sinan Unur, Jan 14, 2005
    #3
  4. thundergnat Guest

    wrote:
    > I'm still having problems opening a file. I looked at the docs and this
    > is what I came up with.


    First of all, every program except the most trivial should start with

    use warnings;
    use strict;

    Those two things will go a long way towards helping fix problems.

    >
    >
    > open (FILE, "> C:\notes\gen.txt") || die ("error $!\n"); #See note 1


    I suspect your main problem is the newline character in the filename.
    Double quotes interpolate, so the \n is read as newline. Use single
    quotes instead. The fact that you are opening the file for output is
    likely going to be a problem too. You would likely be better off with
    3 argument open and lexical file handles also. Actually, it would
    probably be better not to hard code the file name. Feed the file name
    as a command line parameter instead.
    Also, if you leave the newline off the die message, more useful
    information will be supplied.


    open my $file, '<', $ARGV[0] or die "Could not open file. $!";

    >
    >
    > while (<FILE>) { #See note 2


    while (<$file>){
    >
    >
    > s/\<[NR][a\d]\>/ /g #note 3
    >

    Will also catch <Na> and <R0>,<R1>, etc. Ok if that's what you want, but
    you may be better tightening up the assertion. It's not necessary to
    escape the angle brackets either.

    s/<(Ra|N\d)>//;

    >
    > if (^\w\D\D\s\d+:\d+) #note 4


    You description suggests something more like the following. Also, if you
    are just checking if a pattern exists so you can change it if it does,
    you are better off just trying to change it. It will if it exists and
    won't if it doesn't

    s!^(.\w\w)( \d+):(\d+)!$1/$2/$3!;


    All together...

    use warnings;
    use strict;

    open my $file, '<', $ARGV[0] or die "Could not open file. $!";
    while (<$file>){
    s/<(Ra|N\d)>//;
    s!^(.\w\w)( \d+):(\d+)!$1/$2/$3!;
    print;
    }
     
    thundergnat, Jan 14, 2005
    #4
  5. Guest

    What you have written is EXACTLY what I am looking for! I'm assuming
    the print command on the last line is the command that actually
    executes the changes above? My question is by using $ARGV[0] (What
    exactly is this doing in this case) how does the compiler know what
    file $file is? Should I set ARGC[0] equal to the filename in a line
    above? When I run the code as you have shown it I get no output and no
    changes are made to the text file.
    Thanks so much for taking the time to help me out.
    Joe
     
    , Jan 14, 2005
    #5
  6. wrote in news:1105740122.984904.253900
    @c13g2000cwb.googlegroups.com:

    > What you have written is EXACTLY what I am looking for!


    Who is 'you'? What did he/she write? What are you responding to?

    > I'm assuming the print command on the last line is the command
    > that actually executes the changes above?


    This statement is a tautology as the antecedent is false.

    > My question is by using $ARGV[0] (What exactly is this doing in
    > this case)


    See

    perldoc perlvar

    > how does the compiler know what file $file is?


    That is a really philosophical question. I do not see any variable called
    $file in the nonexistent code your are referring to.

    > Should I set ARGC[0] equal to the filename in a line above?


    How would changing a line in your post accomplish anything?

    > When I run the code as you have shown it I get no output and no
    > changes are made to the text file.


    That is to be expected when one runs non-existent code.

    Sinan.
     
    A. Sinan Unur, Jan 14, 2005
    #6
  7. Joe Smith Guest

    Previous post:

    >> open my $file, '<', $ARGV[0] or die "Could not open file. $!";
    >> while (<$file>){
    >> s/<(Ra|N\d)>//;
    >> s!^(.\w\w)( \d+):(\d+)!$1/$2/$3!;
    >> print;
    >> }


    wrote:
    > I'm assuming the print command on the last line is the command
    > that actually executes the changes above?


    No, it does not. The changes are performed by the s/// operators.

    > My question is by using $ARGV[0] (What exactly is this doing in this case)


    @ARGV is a way of passing file names as command-line arguments.
    As in `perl myprog.pl infile.txt`.

    > how does the compiler know what file $file is?


    In the above code, $file is not a file and it is not a file name.
    It is a file handle. The file handle is activated by open()
    and utilized by <$file>.

    > Should I set ARGC[0] equal to the filename in a line above?


    No, you set it by specifying the filename on the command line.
    If wish to hard-code the file name, use

    my $filename = 'inputfile.txt';
    open my $file, '<', $filename or die "Can't open $filename: $!\n";

    > When I run the code as you have shown it I get no output and no
    > changes are made to the text file.


    Since you did not specify a filename on the command line, your
    script is waiting for you to manually type in some lines of text.
    It will continue to read from STDIN (your keyboard) until you
    type the end-of-file character(s), such as Control-D or
    Control-Z + Enter.

    -Joe
     
    Joe Smith, Jan 16, 2005
    #7
    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. Replies:
    0
    Views:
    612
  2. thunk
    Replies:
    1
    Views:
    359
    thunk
    Mar 30, 2010
  3. thunk
    Replies:
    0
    Views:
    536
    thunk
    Apr 1, 2010
  4. thunk
    Replies:
    14
    Views:
    659
    thunk
    Apr 3, 2010
  5. Kamarulnizam Rahim
    Replies:
    4
    Views:
    246
    Robert Klemme
    Jan 28, 2011
Loading...

Share This Page