Professional Web Applications Themes

pattern matching - PERL Beginners

On Dec 5, 2003, at 3:48 PM, B. Fongo wrote: > I went back to my books to refresh my memory on how to use references. > Your suggestion help a lot, but the subroutine returns wrong values. It was intent upon demonstrating an idea to see how much work were we really into. The following actually sorta explains that we have a bit more work to deal with. > I did some small modifications on the codes below > , and tried it. It return perl-5.8.0-80.3.i386.rpm and > samba-2.2.7-5.8.0.i386.rpm, which is wrong because: > 1. The perl version available ...

  1. #21

    Default Re: Pattern matching


    On Dec 5, 2003, at 3:48 PM, B. Fongo wrote:
    > I went back to my books to refresh my memory on how to use references.
    > Your suggestion help a lot, but the subroutine returns wrong values.
    It was intent upon demonstrating an idea to see how
    much work were we really into. The following
    actually sorta explains that we have a bit more
    work to deal with.
    > I did some small modifications on the codes below
    > , and tried it. It return perl-5.8.0-80.3.i386.rpm and
    > samba-2.2.7-5.8.0.i386.rpm, which is wrong because:
    > 1. The perl version available on the ftp server is higher.
    > 2. The samba version already installed is higher than the one on the
    > ftp server.
    [..]
    >> my remote_packages = qw(perl-5.8.0-88.3.i386.rpm
    >> samba-2.2.7-5.7.0.i386.rpm bob-5.3.2.4.1.rpm xml-2.5.2.1.rpm);
    >> my installed_packages = qw(perl-5.8.0-80.3.i386.rpm
    >> samba-2.2.7-5.8.0.i386.rpm bob-5.3.2.4.1.rpm);

    Given these two lists,

    Since the local host has no 'xml' it would not care about
    picking up the "OS" independent version that could be installed.

    At which point we are down to sorting out how to deal
    with the basic naming schema that appareas to be

    <pkg_nam>-<pkg_ver>-<perl_ver>.<OS>.<suffix>

    which of course works for our two canonical bits,
    but not for bob or xml... Bob we have and will need
    to check the server - those seem to be of the form

    <pkg_nam>-<pkg_ver>.<suffix>

    eh no? now all we need is a Hash Of Hashes?

    something that would look like say

    $VAR1 = {
    'perl' => {
    'version' => '5.8.0',
    'full_name' => 'perl-5.8.0-88.3.i386.rpm',
    'subversion' => '88.3'
    },
    'xml' => {
    'version' => '2.5.2.1',
    'full_name' => 'xml-2.5.2.1.rpm'
    },
    'samba' => {
    'version' => '2.2.7',
    'full_name' => 'samba-2.2.7-5.7.0.i386.rpm',
    'subversion' => '5.7.0'
    },
    'bob' => {
    'version' => '5.3.2.4.1.1',
    'full_name' => 'bob-5.3.2.4.1.1.rpm'
    }
    };

    cf:
    <http://www.wetware.com/drieux/pbl/Sys/Admin/version_picker.txt>

    ciao
    drieux

    ---

    caveat Emptor, remember in these days
    of highten security having a version is
    one thing, but having subversion may be
    a crime...


    Drieux Guest

  2. #22

    Default Pattern Matching

    Hi,
    I want to match a pattern and replace with another string.Since the "?"
    is also one of the character present in the string.
    I am not able to do it.
    Please help to how to make "?" acceptable in the string used for pattern
    matching.

    My problem is:
    Here in the script,how to make the Perl to accept "?" symbol in the string
    for pattern matching.

    my $pattern="<a href=\"/getfile.php?id=1894\">Dennis";
    my $replace="<script>\ndoent.write(\"<a href =
    \"url+\"/getfile.php?id=1894\">Dennis";
    open(IN, "<$filetoopen") || die "cannot open file\n";
    open(OUT, ">$test") || die "cannot open file\n";
    while (<IN>){
    if (/$pattern/){
    s/$pattern/$replace/g;
    }

    Tell me in what way I need to give the "?'" symbol in the $pattern string,So
    that string will be accepted.

    With Thanks inadvance,
    Prabu.


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.620 / Virus Database: 399 - Release Date: 3/11/2004



    Prabu Guest

  3. #23

    Default Re: Pattern Matching

    On Apr 6, 2004, at 7:46 AM, prabu wrote:
     

    Howdy.
     

    I bet we can fix that.
     

    First, there is no reason to do two pattern matches here. Drop the if
    (/$pattern/) { }, since it does nothing but waste time.

    The other match is easy to fix:

    s/\Q$patterrn\E/$replace/g;

    Hope that helps.

    James

    James Guest

  4. #24

    Default Pattern Matching

    Hello All,
    Thanks for the useful informations given on my previous queries.Now I
    have another one query.
    Please help me.
    In pattern matching,I want to match a pattern in multiple line.How to do
    this?.
    For example,in a file,the pattern is present in 2 lines,
    Here my doubts are:
    1.Whether need to use "/n" to distinguish between 2 lines,when decalring
    them?
    Or anyother operator needed to be used?.
    (ie) $pattern="text in 1st line "/n" text in secondline";
    $replace="some text";
    2. Whether to specify any options in the "s/$pattern/$replace/g" command?

    Thanks inadvance,
    Prabu.




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.620 / Virus Database: 399 - Release Date: 3/11/2004



    Prabu Guest

  5. #25

    Default pattern matching


    Hi,

    I have a string similar to:
    Comment: FILING
    ****------------------------------------------------------------------------****

    This is read in as one line (with the page feed).
    I was trying to whatever follows the words and I tried this.

    if (/(\w+:?\s*\w*:?)+(.*?\*+\s*-+\s*\*+\s*$)/) {
    s/$2//;
    }

    I am getting errors as the program runs (Windows Perl 5.6.0)

    The program bombs, displays $_ and then,
    /: nested *?+ in regexp at etc.

    I tried taking out some of the pattern to pin point the error, and different errors happened.

    Any suggestions?

    Thanks
    Murzc@aol.com Guest

  6. #26

    Default Re: pattern matching

    On Jun 23, 2004, at 12:34 PM, com wrote:
     

    Let's try to get a little simpler with our approach. Does this grab
    what you need?

    m/(\W+)$/

    James

    James Guest

  7. #27

    Default Re: pattern matching

    (Let's keep our discussion on the list for all to see.)

    On Jun 23, 2004, at 12:44 PM, com wrote:
     


    >
    > What a quick response. Thanks.[/ref]

    No problem.
     

    No, I don't mean that. Perhaps I didn't understand what you needed. I
    thought you wanted to capture the characters after the words.

    Are you trying to remove them instead? If so, use:

    s/\W+$//

    That would replace all non-word characters at the end of your string
    with nothing, deleting them.

    If I'm still not getting it, try explaining your goal again.

    James

    James Guest

  8. #28

    Default Re: pattern matching


    OK. I am reading a file. This line is at the bottom of the
    file and the "****-----------******" is a sign that the section is complete.

    I need to be able to pick up that line, and see if there is
    also code on the beginning of that line that I need to save.
    So in this case I was searching for words (with colon's)
    and perhaps other code that precedes the "****----****"
    and to try and discard the end of the line. (After the word
    FILING, there is a page break character, but it's all being
    read in as one line.)

    This is a sample of the line:
    Comment: FILING ****------------------------------------------------------------------------****

    That's why I tried (and failed :>) with this:
    if (/(\w+:?\s*\w*:?)+(.*?\*+\s*-+\s*\*+\s*)$/) {
    s/$2//;
    }

    I hope that helps.
    Murzc@aol.com Guest

  9. #29

    Default RE: pattern matching

    com wrote: 
    ****-----------------------------------------------------------------------
    -**** 

    Trying to remove?
     
    ^^
    This is no good. 

    Would something like this do the trick?

    $_ = $1 if /((\s*\w+:?)+)/;

    This captures "Comment: FILING"

    HTH
    Bob Guest

  10. #30

    Default Re: pattern matching

    >Would something like this do the trick?
     

    True. But I need to test the line to see if it contains
    those '*****------******'. Otherwise how do I know that
    the line needs to be edited.

    (I am reading a file with many lines. I have to
    detect when the line comes with this pattern.
    Words, colons, a possible page break, followed by "****---****", as indicated in my earlier e mail.)

    Thanks for the time
    PS The reason I put .*? is I wanted to perform a non-greedy
    match on everything from the words until the "*****-----****".
    i am suprised that that would be incorrect.
    Murzc@aol.com Guest

  11. #31

    Default Re: pattern matching

    On Jun 23, 2004, at 12:59 PM, com wrote:
     

    This may be a sign that you aren't reading the file in the easiest
    possible way. I wonder if setting the input separator to this sequence
    would be the way to go. Something like:

    local $/ = '****-----------******';
     

    save( $1 ) if m/^(.+)\Q****-----------******\E$/;
     

    Is that a form feed character? Is it the only one in the file? You
    might be able to use that somehow. Ideas:

    local $/ = "\f";

    # or ...

    save( $1 ) if m/^([^\f]+)\f/;

    # or ...

    my $words = (split m/\f/, $your_line)[0];
     

    Hopefully the above might give you some fresh ideas.

    James

    James Guest

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Pattern matching for xx-xx-xx string
    By jeff@nospam.com in forum PHP Development
    Replies: 7
    Last Post: January 19th, 06:38 PM
  2. Pattern Matching Operators
    By Prasad Karpur in forum PERL Beginners
    Replies: 2
    Last Post: October 5th, 04:21 PM
  3. Pattern matching username
    By perl@swanmail.com in forum PERL Beginners
    Replies: 1
    Last Post: September 30th, 05:37 PM
  4. [PHP] mysql Pattern Matching
    By Cpt John W. Holmes in forum PHP Development
    Replies: 1
    Last Post: September 5th, 06:42 PM
  5. mysql Pattern Matching
    By Electroteque in forum PHP Development
    Replies: 3
    Last Post: September 4th, 03:30 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