Help; Pattern Matching

Discussion in 'Perl Misc' started by MJS, Sep 26, 2003.

  1. MJS

    MJS Guest

    The following is what I have and I can't figure out how to do it. I
    read faq and I don't get the output what I intent to. I am having
    difficulties with proper indentation and white/blank lines.

    User has to enter an integer. Depending on that, the pattern should be
    repeated the number of times the user specified. Everytime the pattern
    is repeated, there should be an increament somewhere in the pattern.
    The output is stored in a file.

    For example: the exact pattern with the spaces is

    "Can someone help
    me with


    problem number1 => "

    This pattern should be replaced by
    "This is
    solution


    number 1, <= "

    "This is
    solution


    number 2, <= "

    and so on depending on the user input.
    ========================================================
    use strict;
    my $number;
    my $pattern;
    my $replace;
    # data.txt is the file containing the text pattern
    open(OUTPUT,"> result.txt") or die("Couldn't open data.txt :$!");

    print "Please enter a positive integer for Address = ";
    chomp( my $number = <STDIN> );

    if ($number =~ tr/1-9// != length ($number)) {
    print "You did not enter an interger";
    }

    else {
    $pattern=~/Can someone help
    me with


    problem number1 =>/;

    $replace=~/"This is
    solution


    number /;
    while(<OUTPUT>){
    for (my $n=1; $n<=$number; $n++){
    s/$pattern/$replace."$n". '<='/gsm;
    }
    }
    }
    close(OUTPUT);
    ======================================
     
    MJS, Sep 26, 2003
    #1
    1. Advertising

  2. MJS

    MJS Guest

    (MJS) wrote in message news:<>...
    > The following is what I have and I can't figure out how to do it. I
    > read faq and I don't get the output what I intent to. I am having
    > difficulties with proper indentation and white/blank lines.
    >
    > User has to enter an integer. Depending on that, the pattern should be
    > repeated the number of times the user specified. Everytime the pattern
    > is repeated, there should be an increament somewhere in the pattern.
    > The output is stored in a file.
    >
    > For example: the exact pattern with the spaces is
    >
    > "Can someone help
    > me with
    >
    >
    > problem number1 => "
    >
    > This pattern should be replaced by
    > "This is
    > solution
    >
    >
    > number 1, <= "
    >
    > "This is
    > solution
    >
    >
    > number 2, <= "
    >
    > and so on depending on the user input.
    > ========================================================
    > use strict;
    > my $number;
    > my $pattern;
    > my $replace;
    > # data.txt is the file containing the text pattern
    > open(OUTPUT,"> result.txt") or die("Couldn't open data.txt :$!");
    >
    > print "Please enter a positive integer for Address = ";
    > chomp( my $number = <STDIN> );
    >
    > if ($number =~ tr/1-9// != length ($number)) {
    > print "You did not enter an interger";
    > }
    >
    > else {
    > $pattern=~/Can someone help
    > me with
    >
    >
    > problem number1 =>/;
    >
    > $replace=~/"This is
    > solution
    >
    >
    > number /;
    > while(<OUTPUT>){
    > for (my $n=1; $n<=$number; $n++){
    > s/$pattern/$replace."$n". '<='/gsm;
    > }
    > }
    > }
    > close(OUTPUT);
    > ======================================


    Any Help????????
     
    MJS, Sep 26, 2003
    #2
    1. Advertising

  3. MJS wrote:
    > The following is what I have and I can't figure out how to do it. I
    > read faq and I don't get the output what I intent to. I am having
    > difficulties with proper indentation and white/blank lines.


    I have a feeling that you're having difficulties with much more than
    that... Actually, I was about to reply to your message before you
    reposted after less than 10 hours, but I didn't know where to start,
    so I didn't.

    I think you need to both structure the problem better and learn some
    more Perl before you are ready to accomplish what you are trying to
    do. As far as I understand, the code you posted isn't even close to
    what you say that you want the script to do.

    Questions about well defined problems are welcome in this group, at
    least as long as they are not FAQs, but requests for help, that in
    effect would be equal to doing the whole work, are not.

    To summarize, I think you need to do some homework before asking for
    help here.

    http://learn.perl.org/

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 26, 2003
    #3
  4. MJS

    MJS Guest

    Rather than telling me to read the stuff, why doesn't anybody tell me
    where I am doing wrong. After reading the stuff, thats what I came up
    with though I admit I lack experience. I still appreciate what you
    have said as you definitely spend your precious time on reply. Thanks
    a lot.
     
    MJS, Sep 27, 2003
    #4
  5. MJS wrote:
    > Rather than telling me to read the stuff, why doesn't anybody tell
    > me where I am doing wrong.


    I would guess it is because you are not doing enough of it right.

    If you are really interested in learning programming, especially
    programming in Perl, and provided that you show that you are making
    efforts to learn, there are quite a few very experienced people here
    who are willing to give a hand.

    > After reading the stuff, thats what I came up with though I admit I
    > lack experience.


    A couple of examples:

    - You mention a file named data.txt, where some pattern is supposed to
    be stored, but you don't open that file.

    - There are both syntax and logic errors, which at least make me
    confused about what exactly you are trying to do.

    You simply need to try harder. Also, reading the posting guidelines
    for this group may give you a better feeling for how to ask questions
    here (later on):
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 27, 2003
    #5
  6. MJS

    Jay Tilton Guest

    (MJS) wrote:

    : Rather than telling me to read the stuff, why doesn't anybody tell me
    : where I am doing wrong.

    Like Gunnar said, there are so many mistakes that it is hard to know
    where to begin. Many of the mistakes are of such a basic nature (like
    confusing the binding operator with the assignment operator) that the
    most logical place to begin is by reading the docs.

    IOW, you need to understand what each piece does before trying to
    construct a complete program from them.

    Also, that the problem is so urgent that it deserved a repost within ten
    hours must have raised the "homework problem" red flag in many readers'
    minds. It is, after all, still September.

    [ content taken from earlier message,
    Message-ID: <> ]

    : use strict;

    use warnings;

    That addition alone would have caused several messages to be emitted.
    Those messages are an invaluable guide to debugging.

    : my $number;
    : my $pattern;
    : my $replace;

    Better to declare lexicals within the smallest scope necessary than to
    give them all file scope like that.

    : # data.txt is the file containing the text pattern
    : open(OUTPUT,"> result.txt") or die("Couldn't open data.txt :$!");
    ^^^^^^^^^^ ^^^^^^^^
    Which filename is correct?

    : print "Please enter a positive integer for Address = ";
    : chomp( my $number = <STDIN> );
    : if ($number =~ tr/1-9// != length ($number)) {
    : print "You did not enter an interger";
    : }

    Integers are not allowed to contain the digit 0 ?

    A better way to disallow non-numeric characters is to simply check that
    the string does not contain them.

    if( $number =~ /\D/ ) { .... }

    : else {
    : $pattern=~/Can someone help
    : me with
    :
    :
    : problem number1 =>/;

    You want to assign a value to $pattern, not bind the match operator to
    it.

    my $pattern = "Can someone help....";

    : $replace=~/"This is
    : solution
    :
    :
    : number /;

    Ditto.

    my $replace = "This is solution....";

    : while(<OUTPUT>){

    The OUTPUT filehandle was opened for output only. Why is the program
    trying to read from it? Are you really trying to do an in-place edit of
    the file?

    : for (my $n=1; $n<=$number; $n++){

    Blecch.

    for my $n (1..$number) {

    : s/$pattern/$replace."$n". '<='/gsm;
    ^^^^
    See perlfaq4, ' What's wrong with always quoting "$vars" '

    Assuming for a moment that the filehandle was open for input and a
    record was read into $_, that record will contain only one line. Your
    pattern spans several lines. The match will never succeed.

    Assuming for a moment that you had slurped the entire file content into
    $_ and the match has a chance of succeeding, the s/// operator will do
    nothing after the first iteration, since you have irreversibly mangled
    $_. Copy the value of $_ into another scalar then mangle the copy.

    The replacement string, $replace."$n". '<=' , is code. Either make it
    an ordinary double-quotish string, e.g.

    s/$pattern/$replace$n<=/g;

    or tack the /e switch onto the s/// operator so the replacement string
    gets evaluated as code.

    On the s/// operator, the /m switch only affects the meaning of the ^
    and $ regex metacharacters and the /s switch only affects the meaning of
    the . regex metacharacter. None of those characters appear in the
    pattern, so those switches can be discarded.

    Did you mean to output the altered string somewhere?

    : }
    : }
    : }
    : close(OUTPUT);
     
    Jay Tilton, Sep 27, 2003
    #6
    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. danpres2k
    Replies:
    3
    Views:
    7,519
    danpres2k
    Aug 25, 2003
  2. Shawna
    Replies:
    3
    Views:
    1,106
    John C. Bollinger
    May 27, 2005
  3. Yoon Soo

    pattern matching code - little help needed

    Yoon Soo, Mar 7, 2004, in forum: C Programming
    Replies:
    0
    Views:
    320
    Yoon Soo
    Mar 7, 2004
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    266
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    268
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page