Professional Web Applications Themes

pattern matching - PERL Beginners

brian liu <xliu75> wrote: > $defaultDir='d:\haha'; or $defaultDir='d:/haha'; > $tempOutput = $defaultDir.'\tempOutput.txt'; or $tempOutput = "$defaultDir/tempOutput.txt"; > local $path=$defaultDir; You should always prefer my() over local(), except when you can't. Space characters are not a scarce resourse, feel free to use as many as you need to make your code easier to read: my $path = $defaultDir; > open DF,$path.'/'.$dataF or print "Error opening file $dataF, at or open DF, "$path/$dataF" or print "Error opening file '$dataF', at > $path \n Error: $!\n"; > while(<DF>){ You _want_ to plow ahead and read nothing from the file if the open() failed? ...

  1. #1

    Default Re: pattern matching

    brian liu <xliu75> wrote:

    > $defaultDir='d:\haha';
    or
    $defaultDir='d:/haha';

    > $tempOutput = $defaultDir.'\tempOutput.txt';
    or
    $tempOutput = "$defaultDir/tempOutput.txt";

    > local $path=$defaultDir;

    You should always prefer my() over local(), except when you can't.

    Space characters are not a scarce resourse, feel free to use
    as many as you need to make your code easier to read:

    my $path = $defaultDir;

    > open DF,$path.'/'.$dataF or print "Error opening file $dataF, at
    or
    open DF, "$path/$dataF" or print "Error opening file '$dataF', at

    > $path \n Error: $!\n";
    > while(<DF>){

    You _want_ to plow ahead and read nothing from the file if
    the open() failed?

    That seems a strange thing to want...

    > if(/^$/){next;}
    or
    next if /^$/;

    or
    next unless length;

    > if(/<TD>(a\d+)<\/TD>/|/<TD>(b)<\/TD>/){
    ^
    ^

    Using the "bitwise or" there is dangerous, you should use a
    "logical or" instead.

    or
    if(/<TD>(a\d+)<\/TD>/ or /<TD>(b)<\/TD>/){
    > a[$count]=$1;

    You should always enable warnings when developing Perl code!

    > if ($2!=null){

    You want to test if $2 is zero?

    Why do you want to test for that?

    If that is what you want, then you should say it more clearly:

    if ( $2 != 0 ) {


    > it seems like after match first <TD></TD> then didn't match the rest
    > of this line, just go to next line

    Yes, that is what it is supposed to do.

    > How can I match first pattern in one line and then match again using
    > same pattern or other pattern in the same line , in perl?

    The way it says to in the description of the m// operator in:

    perldoc perlop

    g Match globally, i.e., find all occurrences.


    --
    Tad McClellan SGML consulting
    [email]tadmcaugustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  2. #2

    Default Re: pattern matching



    Have you looked at the HTML::TableExtract module?

    Or you might p one-dimentional <table>'s just like this:
    Or I sometimes use the following sub to extract html-<table>'s
    and copy the non-html-content inside <td>-tags into
    a list of lists. (Two-dim-perl-array).


    my $html=<<"END";
    <html>
    ...
    The table:
    <table>
    <tr><td><b>Now:</b></td> <td>234</td> <td>11</td></tr>
    <tr><td><b>Then:</b></td> <td><a href="asdf">ABC</a></td></tr>
    <tr><td>After:</td><td>1</td><td>2</td><td>3<td>4<td>5</tr>
    </table>
    </html>
    END
    my table = ht2t($html); #Or ht2t($html,'table:'); #string before right
    <table>
    for (table){ print map "($_) ",$_; print "\n" }
    sub ht2t { # = html-table-to-table
    my($f,$s)=_;
    $f=~s,.*?($s).*?(<table.*?)</table.*,$2,si;
    my $e=0;$e++ while index($f,$s=chr($e))>=$[;
    $f=~s/<t(d|r|h).*?>/$1$s/gsi;
    $f=~s/\s*<.*?>\s*//gsi;
    my t=split("r$s",$f);shift t;
    for (t){my r=split(/[dh]$s/,$_);shift r;$_=[r]}
    t;
    }
    __END__


    This prints, as expected:

    (Now:) (234) (11) (Then:) (ABC)
    (After:) (1) (2) (3) (4) (5)




    On 31 Jul 2003 16:02:51 -0700, brian liu <xliu75> wrote:
    > i got pop0.htm file, I want to get value between <TD> and </TD>
    > and put into one array.
    > like a[0]=a1 a[1]=b a[2]=a2 a[3]=a3
    >
    > pop0.htm
    >
    > <HTML>
    > <HEAD>
    > </HEAD>
    > <BODY bgColor=#ffffff>
    > <TABLE border=1>
    > <TR>
    > <TD>a1</TD>
    > <TD>b</TD> <TD>a2</TD>
    > <TD>a3</TD>
    > </TR>
    > <TABLE>
    > </BODY>
    > </HTML>
    >
    >
    > I use perl code pop0.pl below:
    > $defaultDir='d:\haha';
    > $inputFile='pop\d+\.htm';
    >
    > #Output files
    > $tempOutput = $defaultDir.'\tempOutput.txt';
    >
    > #Clear files
    > unlink $tempOutput;
    >
    > main($defaultDir);
    >
    >
    > sub main()
    > {
    > #$err= opendir THISDIR,$_[0];
    > if( ! opendir THISDIR,$_[0] ) {
    > print "Faild to open $_[0]\n";
    > die;
    > }
    >
    >
    > my dataFiles = grep /$inputFile/ , readdir THISDIR; # Get all
    > data.* files
    > closedir THISDIR;
    > openFiles();
    >
    > sort dataFiles;
    > foreach $file (dataFiles){
    > processFile($file);
    > }
    >
    > closeFiles();
    > print "Done.\n";
    >
    > }#main
    >
    >
    > sub processFile()
    > {
    >
    > local $path=$defaultDir;
    > local $dataF = $_[0];
    > print "Processing $_[0] ...\n";
    >
    > $count=1;
    > open DF,$path.'/'.$dataF or print "Error opening file $dataF, at
    > $path \n Error: $!\n";
    > while(<DF>){
    >
    > if(/^$/){next;}
    > if ($count<5){
    > if(/<TD>(a\d+)<\/TD>/|/<TD>(b)<\/TD>/){
    > a[$count]=$1; if ($2!=null){
    > } print "$count a[$count] \t";
    > $count++;
    > }
    > }
    >
    > }
    > }
    >
    >
    >
    > # --- DB access methods --------
    >
    >
    > sub openFiles()
    > {
    > open S,'>>'.$tempOutput or print "Error opening file $tempOutput \n
    > Error: $!\n";
    > }
    >
    > sub closeFiles()
    > {
    > close S;
    > }
    >
    >
    > and get print out is like a[0]=a1 a[1]=b a[2]=a3 done;
    > it seems like after match first <TD></TD> then didn't match the rest
    > of this line, just go to next line
    >
    > How can I match first pattern in one line and then match again using
    > same pattern or other pattern in the same line , in perl?
    >
    > Thanks a lot
    >
    > Brian
    >


    --
    Using M2, Opera's revolutionary e-mail client: [url]http://www.opera.com/m2/[/url]
    Kjetil Skotheim Guest

  3. #3

    Default Re: pattern matching

    Kjetil Skotheim <kjetilskotheim> wrote in message news:<oprs7lfzdx85yxzqnntp.uio.no>...
    > Have you looked at the HTML::TableExtract module?
    >
    > Or you might p one-dimentional <table>'s just like this:
    > Or I sometimes use the following sub to extract html-<table>'s
    > and copy the non-html-content inside <td>-tags into
    > a list of lists. (Two-dim-perl-array).
    >
    >
    > my $html=<<"END";
    > <html>
    > ...
    > The table:
    > <table>
    > <tr><td><b>Now:</b></td> <td>234</td> <td>11</td></tr>
    > <tr><td><b>Then:</b></td> <td><a href="asdf">ABC</a></td></tr>
    > <tr><td>After:</td><td>1</td><td>2</td><td>3<td>4<td>5</tr>
    > </table>
    > </html>
    > END
    > my table = ht2t($html); #Or ht2t($html,'table:'); #string before right
    > <table>
    > for (table){ print map "($_) ",$_; print "\n" }
    > sub ht2t { # = html-table-to-table
    > my($f,$s)=_;
    > $f=~s,.*?($s).*?(<table.*?)</table.*,$2,si;
    > my $e=0;$e++ while index($f,$s=chr($e))>=$[;
    > $f=~s/<t(d|r|h).*?>/$1$s/gsi;
    > $f=~s/\s*<.*?>\s*//gsi;
    > my t=split("r$s",$f);shift t;
    > for (t){my r=split(/[dh]$s/,$_);shift r;$_=[r]}
    > t;
    > }
    > __END__
    >
    >
    > This prints, as expected:
    >
    > (Now:) (234) (11) (Then:) (ABC)
    > (After:) (1) (2) (3) (4) (5)
    >
    >
    I think this one can work.

    here is another solution:
    $defaultDir='c:\haha';
    $inputFile='pop\d+\.htm';
    #$/="";
    #$/ = '<HTML>';
    #package CPath::WebApp;

    #Output files
    $tempOutput = $defaultDir.'\tempOutput.txt';

    #Clear files
    unlink $tempOutput;

    main($defaultDir);


    sub main()
    {
    #$err= opendir THISDIR,$_[0];
    if( ! opendir THISDIR,$_[0] ) {
    print "Faild to open $_[0]\n";
    die;
    }


    my dataFiles = grep /$inputFile/ , readdir THISDIR; # Get all
    data.* files
    closedir THISDIR;
    openFiles();

    sort dataFiles;
    foreach $file (dataFiles){
    processFile($file);
    }

    closeFiles();
    print "Done.\n";

    }#main


    sub processFile()
    {

    local $path=$defaultDir;
    local $dataF = $_[0];
    print "Processing $_[0] ...\n";

    $count=1;
    open DF,$path.'/'.$dataF or print "Error opening file $dataF, at
    $path \n Error: $!\n";
    while(<DF>){

    if(/^$/){next;}
    if ($count<5){

    if(/<TD>(.*?)<\/TD> <TD>(.*?)<\/TD>/){
    a[$count]=$1;
    a[$count+1]=$2;

    print "$count a[$count] \t";
    print "$count+1 a[$count+1] \t";
    $count+=2;
    next;
    }

    if(/<TD>(.*?)<\/TD>/){
    a[$count]=$1;

    print "$count a[$count] \t";
    $count++;
    }

    }

    }
    }



    # --- DB access methods --------


    sub openFiles()
    {
    open S,'>>'.$tempOutput or print "Error opening file $tempOutput \n
    Error: $!\n";
    }

    sub closeFiles()
    {
    close S;
    }
    brian liu Guest

  4. #4

    Default pattern matching

    I'm not sure how to do this, so I thought i'd try asking here.

    I want to search through a large file and look for strings in the form
    of:
    "{xx-x-x-x-}"

    Lets say the first match I find is:
    B2C12A53-155A-521D-A0205BA35271

    I then need to search for all other instances of this same string.

    now I want to replace all instances of this string with a single new
    string, an integer actually.

    Once all this is done, I need to move on to the next pattern match for
    "{xx-x-x-x-}", and repeat the same process.
    For every loop I need to increment the integer which will replace the
    found string. Incrementing the integer should be pretty easy. But i'm
    kind of lost on the pattern matching and replacing part.

    Any help would be greatly appreciated.
    Matthew Guest

  5. #5

    Default Re: pattern matching

    [email]yk7n6yo102sneakemail.com[/email] (Matthew) wrote in message news:<2309ae4b.0310220012.30571ff0posting.google. com>...
    > I'm not sure how to do this, so I thought i'd try asking here.
    You should probably have started a new thread.
    > I want to search through a large file and look for strings in the form
    > of:
    > "{xx-x-x-x-}"
    Ok, so your pattern is
    my $pat = qr/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/;

    ....
    > now I want to replace all instances of this string with a single new
    > string, an integer actually.
    > Once all this is done, I need to move on to the next pattern match for
    > "{xx-x-x-x-}", and repeat the same process.
    > For every loop I need to increment the integer which will replace the
    > found string. Incrementing the integer should be pretty easy. But i'm
    > kind of lost on the pattern matching and replacing part.
    Going through the file, catalogue all new patterns, then replace all
    patterns with their replacements:
    my %seen = ();
    my $sequence = 0;
    while (<FILE>) {
    while (/($pat)/go) {
    $seen{$1} ||= ++$sequence;
    }
    s/($pat)/$seen{$1}/g;
    }
    Roy Johnson Guest

  6. #6

    Default pattern matching

    Hi everybody!

    Two questions:
    I've got a test-file and want to add something to some lines, while
    reading it out.
    I tried the following code:

    while (<TXTFILE>) {
    if (/pattern/) {
    #$line = $_ . $something;
    #push (new_array, $line);
    }
    else {
    #$line = $_;
    #push (new_array, $line);
    }
    }

    But new_array didn't get the lines!

    The textfile look like
    >SA texttexttext...\n
    texttexttext\n
    texttexttext\n
    texttexttext\n
    >SA texttexttext...\n
    texttexttext\n
    texttexttext\n
    texttexttext\n
    >SA texttexttext...\n
    texttexttext\n
    texttexttext\n
    texttexttext\n

    I tried to get the >SA..-lines by

    if (/^SA[.]*\n/) {
    do...

    but the pattern was not be found.
    Whats wrong with it?

    Or does anyone knows a simple method to read the lines in a hash -
    every-SA-line should be the key, the following lines until the next >SA
    the value. That'll be much more simple to handle..

    Jane



    Christiane Nerz Guest

  7. #7

    Default RE: pattern matching

    > Two questions:
    > I've got a test-file and want to add something to some lines, while
    > reading it out.
    > I tried the following code:
    >
    > while (<TXTFILE>) {
    > if (/pattern/) {
    > #$line = $_ . $something;
    > #push (new_array, $line);
    > }
    > else {
    > #$line = $_;
    > #push (new_array, $line);
    > }
    > }
    The code that appends the array is commented out...... if I assume that you have tried it without the comments, try putting a print statement inside the 'if' block, this will atleast let you know if your regexp is correct.

    __________________________________________________ ______________________
    This email has been scanned for all viruses by the MessageLabs Email
    Security System. For more information on a proactive email security
    service working around the clock, around the globe, visit
    [url]http://www.messagelabs.com[/url]
    __________________________________________________ ______________________
    Stephen Hardisty Guest

  8. #8

    Default Re: pattern matching

    SORRY! copy-and-paste-error...I tried it without the ### ;-)
    and tried too the print-statement-thing.
    Seems the regex is wrong, but why?
    Jane





    Stephen Hardisty wrote:
    >>Two questions:
    >>I've got a test-file and want to add something to some lines, while
    >>reading it out.
    >>I tried the following code:
    >>
    >>while (<TXTFILE>) {
    >> if (/pattern/) {
    >> #$line = $_ . $something;
    >> #push (new_array, $line);
    >> }
    >> else {
    >> #$line = $_;
    >> #push (new_array, $line);
    >> }
    >>}
    >>
    >
    > The code that appends the array is commented out...... if I assume that you have tried it without the comments, try putting a print statement inside the 'if' block, this will atleast let you know if your regexp is correct.
    >
    > __________________________________________________ ______________________
    > This email has been scanned for all viruses by the MessageLabs Email
    > Security System. For more information on a proactive email security
    > service working around the clock, around the globe, visit
    > [url]http://www.messagelabs.com[/url]
    > __________________________________________________ ______________________
    >
    >


    Christiane Nerz Guest

  9. #9

    Default RE: pattern matching

    > SORRY! copy-and-paste-error...I tried it without the ### ;-)
    > and tried too the print-statement-thing.
    > Seems the regex is wrong, but why?
    > Jane
    Try:
    /^SA.+\n/

    __________________________________________________ ______________________
    This email has been scanned for all viruses by the MessageLabs Email
    Security System. For more information on a proactive email security
    service working around the clock, around the globe, visit
    [url]http://www.messagelabs.com[/url]
    __________________________________________________ ______________________
    Stephen Hardisty Guest

  10. #10

    Default Re: pattern matching

    Christiane Nerz wrote:
    > Hi everybody!
    >
    > Two questions:
    > I've got a test-file and want to add something to some lines, while
    > reading it out.
    > I tried the following code:
    >
    > while (<TXTFILE>) {
    > if (/pattern/) {
    > #$line = $_ . $something;
    > #push (new_array, $line);
    > }
    > else {
    > #$line = $_;
    > #push (new_array, $line);
    > }
    > }
    >
    > But new_array didn't get the lines!
    >
    > The textfile look like
    >
    > >SA texttexttext...\n
    > texttexttext\n
    > texttexttext\n
    > texttexttext\n
    > >SA texttexttext...\n
    > texttexttext\n
    > texttexttext\n
    > texttexttext\n
    > >SA texttexttext...\n
    > texttexttext\n
    > texttexttext\n
    > texttexttext\n
    >
    > I tried to get the >SA..-lines by
    >
    > if (/^SA[.]*\n/) {
    > do...
    >
    > but the pattern was not be found.
    > Whats wrong with it?
    >
    > Or does anyone knows a simple method to read the lines in a hash -
    > every-SA-line should be the key, the following lines until the next >SA
    > the value. That'll be much more simple to handle..
    >
    > Jane
    >
    >
    >
    Read about push
    perldoc -f push


    Maybe this will help

    ......
    while(<TXTFILE>) {
    if(/pattern/){
    $line = $_ . $something;
    } else {
    $line = $_;
    }

    push newarray , $line
    ......




    Ramprasad A Padmanabhan Guest

  11. #11

    Default Re: pattern matching

    Christiane Nerz wrote:
    >
    > Hi everybody!
    Hello,
    > Two questions:
    > I've got a test-file and want to add something to some lines, while
    > reading it out.
    > I tried the following code:
    >
    > while (<TXTFILE>) {
    > if (/pattern/) {
    > #$line = $_ . $something;
    > #push (new_array, $line);
    > }
    > else {
    > #$line = $_;
    > #push (new_array, $line);
    > }
    > }
    >
    > But new_array didn't get the lines!
    >
    > The textfile look like
    >
    > >SA texttexttext...\n
    > texttexttext\n
    > texttexttext\n
    > texttexttext\n
    > >SA texttexttext...\n
    > texttexttext\n
    > texttexttext\n
    > texttexttext\n
    > >SA texttexttext...\n
    > texttexttext\n
    > texttexttext\n
    > texttexttext\n
    >
    > I tried to get the >SA..-lines by
    >
    > if (/^SA[.]*\n/) {
    > do...
    >
    > but the pattern was not be found.
    > Whats wrong with it?
    Your pattern specifies that the line must start with the characters 'S'
    and 'A' followed by zero or more of the '.' character followed by a
    newline character but your example shows that the line starts with the
    characters ' ', '>', 'S', 'A' and ' '.

    while ( <TXTFILE> ) {
    if ( /^ >SA .*\n/ ) {
    push new_array, $_ . $something;
    }
    else {
    push new_array, $_;
    }
    }

    > Or does anyone knows a simple method to read the lines in a hash -
    > every-SA-line should be the key, the following lines until the next >SA
    > the value. That'll be much more simple to handle..
    my ( $key, %hash );
    while ( <TXTFILE> ) {
    if ( /^ >SA .*\n/ ) {
    $key = $_;
    }
    elsif ( defined $key ) {
    $hash{ $key } .= $_;
    }
    }


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  12. #12

    Default Pattern Matching

    Hi, I'm trying to find out how many newline characters are in a string. I
    thought there would be a simple function for this, but I can't find it;
    Do I need to step through the string a character at a time to check this?
    Rob Guest

  13. #13

    Default Re: Pattern Matching

    On Wed, Nov 19, 2003 at 01:33:25PM -0500, Rob wrote:
    > Hi, I'm trying to find out how many newline characters are in a string. I
    > thought there would be a simple function for this, but I can't find it;
    > Do I need to step through the string a character at a time to check this?
    Here is an example of the Perl idiom that will do this for you. I had a
    file with 11 newlines, which I read into a string. I then used a
    regular expression to match newlines in that string and then took
    advantage of Perl's list context to get the count.

    perl -le'{local $/ = undef; $file = <>;} $number = () = $file =~ /\n/g;
    print $number;' file

    In a non-one-liner, the important bit would be declared with my

    my $number = () = $file =~ /\n/g

    which is equivalent to

    my array = $file =~ /\n/g;
    my $number = array;

    You just save yourself having to explicitly mention the list/array.

    Cheers,


    Damon


    --

    Damon Allen Davison
    [url]http://allolex.freeshell.org/[/url]

    A language is therefore a horizon, and style a vertical
    dimension, which together map out for the writer a Nature,
    since he does not choose either. The language functions
    negatively, as the initial limit of the possible, style is a
    Necessity which binds the writer's humour to his form of
    expression. In the former, he finds a familiar History, in
    the latter, a familiar personal past. In both cases he
    deals with a Nature, that is, a familiar repertory of
    gestures, a gestuary, as it were, in which the energy
    expended is purely operative, serving here to enumerate,
    there to transform, but never to appraise or signify a
    choice.

    -- Roland Barthes (1915-1980), French
    semiologist. ``What Is Writing??'' Writing
    Degree Zero (1953, trans. 1967).
    Damon Allen Davison Guest

  14. #14

    Default RE: Pattern Matching

    > Hi, I'm trying to find out how many newline characters are in
    > a string. I thought there would be a simple function for
    > this, but I can't find it; Do I need to step through the
    > string a character at a time to check this?
    I have used this:

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

    my $string = "newline\nstring\nhere\n";

    print (my $count = $string =~ s/\n/\n/g);

    But I've always thought there must be a more straightforward way.. This
    definitely seems a little clunky. Anybody else have something for this?

    Thanks
    Matt
    Matthew Shaw Guest

  15. #15

    Default Re: Pattern Matching

    This is what I finally came up with, but it too seems rather clunky.

    my $cnt = 0;
    $_ = $notes;
    $cnt = tr/\n//;

    --
    Rob

    On Wed, 19 Nov 2003, Rob wrote:
    > Hi, I'm trying to find out how many newline characters are in a string. I
    > thought there would be a simple function for this, but I can't find it;
    > Do I need to step through the string a character at a time to check this?
    >
    Rob Guest

  16. #16

    Default Re: Pattern Matching

    On Nov 19, Rob said:
    >This is what I finally came up with, but it too seems rather clunky.
    >
    >my $cnt = 0;
    >$_ = $notes;
    >$cnt = tr/\n//;
    Yeah; why not:

    my $count = ($notes =~ tr/\n//);

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  17. #17

    Default Re: Pattern Matching

    Rob wrote:
    > This is what I finally came up with, but it too seems rather clunky.
    >
    > my $cnt = 0;
    > $_ = $notes;
    > $cnt = tr/\n//;
    >
    clunky? what's your definition of not clunky? shorter one:

    my $count = $notes =~ y/\n//;

    david
    --
    s,.*,<<,e,y,\n,,d,y,.s,10,,s
    ..ss.s.s...s.s....ss.....s.ss
    s.sssss.sssss...s...s..s....
    ....s.ss..s.sss..ss.s....ss.s
    s.sssss.s.ssss..ss.s....ss.s
    ...s..sss.sssss.ss.sss..ssss.
    ...sss....s.s....ss.s....ss.s

    ,....{4},"|?{*=}_'y!'+0!$&;"
    ,ge,y,!#:$_(-*[./<-{-},b-t,
    ..y...,$~=q~=?,;^_#+?{~,,$~=~
    y.!-&*-/:-^_{}.a-t ().;s,;,
    );,g,s,s,$~s,g,y,y,%,,g,eval
    David Guest

  18. #18

    Default Pattern matching

    Hi

    I'm writing a script that will connect to an ftp server (Redhat mirror),
    and download new versions of all packages that are on my machine. First
    of all, the script collects information about all the packages installed
    on my machine in an array (installed_packages). Then its goes on to get
    a list of all packages in a direcory on the ftp server and store it in
    an array (remote_packages).

    The next step is for me to select those packages from remote_packages
    if found in installed_packages and newer that the one in
    installed_packages.

    for instance:

    remote_packages = qw(perl-5.8.0-88.3.i386.rpm samba-2.2.7-5.7.0.i386.rpm);
    installed_packages = qw(perl-5.8.0-80.3.i386.rpm
    samba-2.2.7-5.8.0.i386.rpm);

    In this example, the remote package perl is newer, so it should be
    stored in a third array; to be download. The below isn't complete, and
    I'm not sure of how to get the pattern matching correctly.


    ==========================================

    sub select_newer {
    my (remote_packages, installed_packages);
    (remote_packages, installed_packages) = _;
    foreach (installed_packages){
    my $i = grep {$_} remote_packages && ;
    }

    I'll appreciate any help

    Babs

    B. Fongo Guest

  19. #19

    Default Fwd: Pattern matching



    Begin forwarded message:
    > From: drieux <drieuxwetware.com>
    > Date: December 4, 2003 8:11:46 PM PST
    > To: "B. Fongo" <mygrpsfongo.de>
    > Subject: Re: Pattern matching
    >
    >
    > On Dec 4, 2003, at 7:09 PM, B. Fongo wrote:
    >
    >>
    >> sub select_newer {
    >> my (remote_packages, installed_packages);
    >> (remote_packages, installed_packages) = _;
    >> foreach (installed_packages){
    >> my $i = grep {$_} remote_packages && ;
    >> }
    >>
    >> I'll appreciate any help
    >
    > that's not going to work the way you would like
    > since perl does not take in two arrays like that.
    >
    > What you will need is a bit of indirection
    > by dealing with references.
    >
    > sub select_newer {
    > my ($remote_packages, $installed_packages) = _;
    > foreach ($installed_packages){
    > my $i = grep {$_} $remote_packages && ;
    > }
    >
    > You can now pass in the two arrays as
    >
    > my $need_list = select_newer(\have_list, \installed_list);
    >
    > then iterate over it in the class form of
    >
    > foreach my $pkg ( $need_list)
    > {
    > # do the stuf with it.
    > }
    >
    > That being said, you might want to think about a
    > strategy of something like
    >
    > 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);
    >
    > 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);
    >
    >
    > my $need_list = check_lists(\remote_packages , \installed_packages);
    >
    > foreach my $pkg ($need_list)
    > {
    > print "need $pkg\n";
    > }
    >
    > #------------------------
    > #
    > sub check_lists
    > {
    > my ($src, $dst) = _;
    >
    > #Making a quick pick hash
    > my %src_hash = map { $_ => 1 } $src;
    >
    > my need_list;
    >
    > foreach my $key ( $dst )
    > {
    > push(need_list, $key)
    > unless(exists($src_hash{$key}));
    > }
    >
    > \need_list;
    >
    > } # end of check_lists
    >
    > Or are you expecting that you MIGHT get into the case where
    > you have downloaded and installed 'a newer version' say
    > the case where you installed
    >
    > bob-5.4.pm
    >
    > and do not want to 'back rev' yourself??
    >
    > ciao
    > drieux
    >
    > ---
    >
    >

    ciao
    drieux

    ---

    Drieux Guest

  20. #20

    Default Re: Pattern matching

    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. 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);
    >
    >
    > # I expect perl-5.8.0-88.3.i386.rpm to be returned here.
    >
    > my $need_list = check_lists(\remote_packages ,
    > \installed_packages);
    >
    > foreach my $pkg ($need_list)
    > {
    #
    $ftp_server->get($pkg) ;
    >
    > }
    >
    > #------------------------
    > #
    > sub check_lists
    > {
    > my ($src, $dst) = _;
    >
    > #Making a quick pick hash
    > my %src_hash = map { $_ => 1 } $src;
    >
    > my need_list;
    >
    > foreach my $key ( $dst )
    > {
    > push(need_list, $key)
    > unless(exists($src_hash{$key}));

    >
    > }
    > # It should return only packages that are higher than those
    > I've already installed.
    > \need_list;
    >
    > } # end of check_lists
    >
    >
    >
    > ---
    >
    >

    B. Fongo Guest

Page 1 of 2 12 LastLast

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