Professional Web Applications Themes

My first perl script - PERL Beginners

Dear All I am a beginner with perl. I would like to ask you whether it is difficult to make a perl script doing the following: (1) read a text file; (2) erase the character # placed at the beginning of the line that contains "Zyloric"; (3) replace the original file with the file got after the deletion mentioned in (2). Thanks in advance, Paul...

  1. #1

    Default My first perl script

    Dear All

    I am a beginner with perl. I would like to ask you whether it is
    difficult to make a perl script doing the following:

    (1) read a text file;
    (2) erase the character # placed at the beginning of the line that
    contains "Zyloric";
    (3) replace the original file with the file got after the deletion
    mentioned in (2).

    Thanks in advance,

    Paul

    Paul Guest

  2. #2

    Default Re: My first perl script

    On Sunday 11 July 2004 10:18, Paul Smith wrote: 

    Hello,
     

    No, it is not that difficult at all, in fact the following "script"
    will do just that:

    perl -i.bak -pe'/Zyloric/&&s/^#//' text_file


    However, if you want a complete Perl program, you could write it
    something like this:

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

    ( $^I, ARGV ) = ( '.bak', 'text_file' );
    while ( <> ) {
    s/^#// if /Zyloric/;
    print;
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment

    John Guest

  3. #3

    Default Re: My first perl script

    Paul Smith wrote: 

    I for one don't think it's difficult. Time to start working and
    finding out for yourself, eh?

    perldoc perlintro
    perldoc Tie::File

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  4. #4

    Default Re: My first perl script

    John W.Krahn wrote: 

    Thanks, John and Gunnar. (Please, be patient with my ignorance.) How can
    one refer, inside the loop while, to the next line in the text file? To
    be more precise, I would like to remove the character # in the next line
    to the one containing "Zyloric". I could use an auxiliary variable, but
    is there some easier way?

    Paul

    Paul Guest

  5. #5

    Default Re: My first perl script

    Paul Smith wrote: 

    You're welcome.
     

    I for one am, don't worry. But I'm *not* patient with suspected
    laziness. ;-)

    I'd like to see you post some code, where you show us that you are
    seriously trying to learn, and where you have at least tried to solve
    this new problem.
     

    See above.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  6. #6

    Default Re: My first perl script

    On Sunday 11 July 2004 14:12, Paul Smith wrote: 
    >[/ref]
    b> Thanks, John and Gunnar. (Please, be patient with my ignorance.) How 

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

    ( $^I, ARGV ) = ( '.bak', 'text_file' );
    while ( <> ) {
    if ( /Zyloric/ ) {
    print;
    ( $_ = <> ) =~ s/^#//;
    }
    print;
    }

    __END__


    Or as a one-liner:

    perl -i.bak -pe'/Zyloric/&&print&&($_=<>)=~s/^#//' text_file



    John
    --
    use Perl;
    program
    fulfillment

    John Guest

  7. #7

    Default Re: My first perl script

    >> (Please, be patient with my ignorance.) 

    Thanks, Gunnar. I understand your point of view. Meanwhile, John Krahn
    has kindly provided a solution for my problem, which I very much thank.
    However, to convince you that I was trying to solve the problem by
    myself, I quote below the script that I have written and that also
    solves the problem:

    Paul
    -------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    $a = 0;

    ( $^I, ARGV ) = ( '.bak', '/home/paul/.reminders' );
    while ( <> ) {
    if ($a == 1) {
    s/^#//;
    print;
    $a = 0;
    }
    elsif (/Zyloric/) {
    s/^#//;
    print;
    $a = 1;
    }
    else {
    print;
    }
    }

    Paul Guest

  8. #8

    Default Re: My first perl script

    On Monday 12 July 2004 01:47, Paul Smith wrote: 

    Sorry, I like to tweek code. You could shorten that a bit:

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

    ( $^I, ARGV ) = ( '.bak', '/home/paul/.reminders' );
    while ( <> ) {
    if ( $a or /Zyloric/ ) {
    s/^#//;
    $a = !$a;
    }
    print;
    }

    __END__


    :-)

    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. [Perl-beginners] Perl script to switch user to root.
    By Remko in forum PERL Beginners
    Replies: 2
    Last Post: March 4th, 06:17 PM
  2. Control a non-perl image viewer from perl script
    By Harry Putnam in forum PERL Beginners
    Replies: 1
    Last Post: October 31st, 10:28 PM
  3. Perl Help - Windows Perl script accessing a Unix perl Script
    By dpackwood in forum PERL Miscellaneous
    Replies: 8
    Last Post: September 30th, 07:54 AM
  4. ASP --> PERL SCRIPT HELP>>PLEASE>>
    By kalusalu in forum PERL Miscellaneous
    Replies: 2
    Last Post: July 8th, 08:19 PM
  5. Execute shell script from a perl script
    By kderaedt in forum Perl / CGI
    Replies: 3
    Last Post: June 26th, 08:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139