[24926] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 7176 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Sep 25 11:06:55 2004

Date: Sat, 25 Sep 2004 08:05:12 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 25 Sep 2004     Volume: 10 Number: 7176

Today's topics:
        [newbie] decode the form information <dontmesswithme@got.it>
    Re: [newbie] decode the form information <uri@stemsystems.com>
    Re: [newbie] decode the form information <dontmesswithme@got.it>
    Re: [newbie] decode the form information <sbryce@scottbryce.com>
    Re: Continous Looping of a List (Anno Siegel)
        few question about array (Andrea Spitaleri)
    Re: few question about array <jkeen_via_google@yahoo.com>
    Re: Help with my brute force method (Anno Siegel)
    Re: perlXStut <kalinaubears@iinet.net.au>
        Printing regex match <dave@home.net>
    Re: Printing regex match <eric-amick@comcast.net>
    Re: Printing regex match <noreply@gunnar.cc>
    Re: Printing regex match (Anno Siegel)
        what's wrong in this Perl Regex expression? <end@dream.life>
    Re: what's wrong in this Perl Regex expression? <tore@aursand.no>
    Re: what's wrong in this Perl Regex expression? <end@dream.life>
    Re: what's wrong in this Perl Regex expression? <someone@example.com>
    Re: what's wrong in this Perl Regex expression? (Mr. M.J. Lush)
    Re: what's wrong in this Perl Regex expression? <tore@aursand.no>
    Re: what's wrong in this Perl Regex expression? <end@dream.life>
    Re: what's wrong in this Perl Regex expression? (Mr. M.J. Lush)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 25 Sep 2004 05:38:54 GMT
From: Larry <dontmesswithme@got.it>
Subject: [newbie] decode the form information
Message-Id: <dontmesswithme-CF8AEB.07354225092004@twister2.tin.it>

Hi folks!

I've a couple of questions i wanna ask you:

1) I've been using the code above to decode the form information.
Unfortunately if I call "use strict" at the top of the script it all 
wretchedly fail and I get a 500 error from the server...

2) How would you go about setting a postmax value in the 
"parse_form_data" of the script? (say,something like $CGI::POST_MAX=1024 
* 150;)

(please don't reply telling me to use CGI.pm because i need to get this 
code working)

[code]
#!/usr/bin/perl

&parse_form_data (*simple_form);

my $user = $simple_form{'user'};

print "Content-type: text/plain", "\n\n";
$user = $simple_form{'user'};
if ($user) {
    print "Nice to meet you ", $simple_form{'user'}, ".", "\n";
    print "Please visit this Web server again!", "\n";
} else {
    print "You did not enter a name. Are you shy?", "\n";
    print "But, you are welcome to visit this Web server again!", "\n";
}
exit(0);

sub parse_form_data
{
    
    local (*FORM_DATA) = @_;
    local ( $request_method, $query_string, @key_value_pairs,
                  $key_value, $key, $value);

    $request_method = $ENV{'REQUEST_METHOD'};
    if ($request_method eq "GET") {
        $query_string = $ENV{'QUERY_STRING'};
    } elsif ($request_method eq "POST") {
        read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
    } else {
        &return_error (500, "Server Error",
                            "Server uses unsupported method");
    }

    @key_value_pairs = split (/&/, $query_string);
    foreach $key_value (@key_value_pairs) {
        ($key, $value) = split (/=/, $key_value);
        $value =~ tr/+/ /;
        $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;

    if (defined($FORM_DATA{$key})) {
            $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value);
        } else {
                    $FORM_DATA{$key} = $value;
        }
    }
}
[/code]

thanks!


------------------------------

Date: Sat, 25 Sep 2004 06:23:38 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: [newbie] decode the form information
Message-Id: <x77jqiyijc.fsf@mail.sysarch.com>

>>>>> "L" == Larry  <dontmesswithme@got.it> writes:

  L> 1) I've been using the code above to decode the form information.
  L> Unfortunately if I call "use strict" at the top of the script it all 
  L> wretchedly fail and I get a 500 error from the server...

  L> 2) How would you go about setting a postmax value in the 
  L> "parse_form_data" of the script? (say,something like $CGI::POST_MAX=1024 
  L> * 150;)

  L> (please don't reply telling me to use CGI.pm because i need to get this 
  L> code working)

use CGI.pm

there is no worldy reason you must get this code to work that would
cause you to not want to use cgi.pm. the code shown is pure perl4,
highly broken and about 10 years out of date.

now it is your turn to whine that you said you can't use cgi.pm. and i
will just reply that you don't know how to fix this code and cgi.pm
already fixes it. then you will say you have to run this under
perl4. and i will say then upgrade. then you will say this is all your
cheapo web hoster runs. i will reply then get a modern web host. you
will then flame about why doesn't anyone answer the original
question. hopefully some of the other cabal^Wregulars chime in about
using cgi.pm and wondering about your iq as compared to W.

so instead of having that long, repetative, deja vu^Wgoogle group
archived flame fest, just shut up and use cgi.pm. i am just trying to
save your skin and lower usenet bandwidth use at the same time.

i used PSI::ESP to prognosticate how this thread will devolve. just
another great module found on cpan (comprehensive psychic archive
network).

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Sat, 25 Sep 2004 09:46:25 GMT
From: Larry <dontmesswithme@got.it>
Subject: Re: [newbie] decode the form information
Message-Id: <dontmesswithme-413F09.11431325092004@twister2.tin.it>

In article <x77jqiyijc.fsf@mail.sysarch.com>,
 Uri Guttman <uri@stemsystems.com> wrote:


> 
> so instead of having that long, repetative, deja vu^Wgoogle group
> archived flame fest, just shut up and use cgi.pm. i am just trying to
> save your skin and lower usenet bandwidth use at the same time.
> 

hey man...you are right!


------------------------------

Date: Sat, 25 Sep 2004 08:39:46 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: [newbie] decode the form information
Message-Id: <10lb0pgo4ic64f5@corp.supernews.com>

Larry wrote:


> (please don't reply telling me to use CGI.pm because i need to get this 
> code working)

Use cgi.pm. It is the best way to get this code working.



------------------------------

Date: 25 Sep 2004 12:27:51 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Continous Looping of a List
Message-Id: <cj3o87$pck$1@mamenchi.zrz.TU-Berlin.DE>

cp  <cpryce@nospam.pryce.net> wrote in comp.lang.perl.misc:
> In article <KW_4d.173$va.125@trndny03>, Paul Lalli <mritty@gmail.com>
> wrote:
> 
> > > I have a list of ids from a database, and a value for the currently
> > > selected value. I need the previous value (or the index of the
> > previous
> > > value) and the next value in the list.
> > >
> > > The ids will not neccessarily be in sequence. The function is called
> > > once, so the original list does not need to be preserved. The list is
> > > arbitrarily long.
> > >
> > > I.e., given a list: 1, 3, 5, 7, 9 and the currently selected id is
> > > value is 5, I need 3 and 7.
> 
> My very bad. In my original problem statement, I neglected to mention
> that I need the loop to be continuous. So if the current selected id is
> 9, the functions needs to return 7 and 1. If the current selection is
> 1, it needs to return 9 and 3. 

Ah, so you want cyclic behavior of the array.  The keyword "cyclic"
should always trigger the notion of "modulo" (just like "unique"
triggers "hash").

[Paul Lalli speaking]

> > My version of your function:
> > 
> > sub array_nav {
> >     my ($val, @ids) = @_;
> >     my $pos;
> >     for ($pos=0; $pos<=$#ids; $pos++){
> >       last if $val == $ids[$pos];
> >     }
> >     return undef unless $pos < @ids;  #error checking
> 
> Yes. I see what you are doing. however, I was hoping for error checking
> to determine whether the selected id was part of the set Before
> looping. May fault for not stating the problem more clearly.

How would you do that?  To determine that an element is not in an array
you will have to inspect all array elements.  There's got to be some
loop.

> >     my $prev = $ids[$pos-1];
> >     my $next = $pos == $#ids ? $ids[0] : $ids[$pos+1];

The modulo function makes this simpler:

    my $prev = $ids[ ($pos - 1) % @ids]; # for symmetry
    my $next = $ids[ ($pos + 1) % @ids]; # simplification

> >     return ($prev, $next);
> > }

You can, however hide the loop.  In my solution it is hidden in a hash
slice assignment.

    sub array_nav {
        my ( $val, @ids) = @_;
        my %inv;
        @inv{ @ids} = 0 .. $#ids; # here is the invisible loop
        defined( my $i = $inv{ $val}) or return;
        @ids[ ($i - 1) % @ids, ($i + 1) % @ids];
    }

The hash approach has the advantage that it can easily be modified
to check if the values in @ids are unique.

Anno


------------------------------

Date: 25 Sep 2004 05:26:02 -0700
From: spiritelllo@interfree.it (Andrea Spitaleri)
Subject: few question about array
Message-Id: <4de1519a.0409250426.6ddf2f72@posting.google.com>

Hi I have few quick question about array:
1. reading a file as array, that below code works but I want be sure
that it is correct that portion
I am in doubt about the $numb[$countmol].
open (IN,"in") || die "$!";
@in=<IN>;
clode IN;
my $i;
for ($i=0;$i<$#in;++$i){
    chomp $i;
    my $countmol=0;
     if($in[$i]=~ /^@<TRIPOS>ATOM/){
        $initial=$i;
        $endmol=$number_atoms + $i;
        for($k=$initial;$k<$endmol;++$k){
            @coord=split(/ +/,$inmol[$k+1]);
#that is it correct?? $numb[$countmol] is different from $numb[$i].
            $numb[$countmol]=$coord[1];
            $atom[$countmol]=$coord[2];
           $x[$countmol]=$coord[3];
           $y[$countmol]=$coord[4];
           $z[$countmol]=$coord[5];
           $atype[$countmol]=$coord[6];
           $atype[$countmol]=$coord[6];
            $charge[$countmol]=$coord[9];
            ++$countmol;
        }
    }
}
2. If I want create an output like that:
1 2 3
4 5 6
7 8 9
 ........
how is the code?

thanks a lot
cheers
and


------------------------------

Date: Sat, 25 Sep 2004 14:27:55 GMT
From: Jim Keenan <jkeen_via_google@yahoo.com>
Subject: Re: few question about array
Message-Id: <Lff5d.2386$r%4.629@trndny05>

Andrea Spitaleri wrote:
> Hi I have few quick question about array:
> 1. reading a file as array, that below code works but I want be sure
> that it is correct that portion
> I am in doubt about the $numb[$countmol].
> open (IN,"in") || die "$!";
> @in=<IN>;
> clode IN;

Have you read the Posting Guidelines for comp.lang.perl.misc?  If you 
had, you would have read that before posting a question to the list, you 
should have tested your code using the following Perl pragmas:

     use strict;
     use warnings;

If you had done so, you would have picked up a typing error in the last 
line of code above.  Before you seek help from others, get as much help 
as you can from Perl itself.

Jim Keenan


------------------------------

Date: 25 Sep 2004 11:15:26 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Help with my brute force method
Message-Id: <cj3k0e$mvv$1@mamenchi.zrz.TU-Berlin.DE>

krakle <krakle@visto.com> wrote in comp.lang.perl.misc:
> larryj@gsu.edu (Larry Felton Johnson) wrote in message
> news:<4ae7bf57.0409231037.7ecf1515@posting.google.com>...
> > krakle@visto.com (krakle) wrote in message 
> > > 
> > > Do you leave your home door wide open when you enter your house?
> > 
> > It depends on the weather, the time of year, whether I plan on being in
> > the front room for the evening, and a number of other factors :-)
> > 
> > You wanna elaborate, or should I continue with domestic habit flow?
> > 
> > Larry
> 
> Your other posts are too long to interest me. What I meant was...
> close the opened file when you are done.. :)

Why?

Anno


------------------------------

Date: Sat, 25 Sep 2004 21:03:13 +1000
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: perlXStut
Message-Id: <415551a5$0$29403$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

Ketema wrote:
> I am attempting to go through the perlXStut and I can't complete the
> make process.  My system is WinXP.  I have Visual Studio.Net 2003
> installed, so I am using nmake, as also confirmed by perl -V:make.  My
> perl is Activestate v5.8.4.
> My perl -V:libc is msvcrt.lib.
> 
> After having a buch of files missing I finally figured out to add
> "c:\Program Files\Microsoft Visual Studio .NET
> 2003\Vc7\bin","c:\Program Files\Microsoft Visual Studio .NET
> 2003\Vc7\include" and "c:\Program Files\Microsoft Visual Studio .NET
> 2003\Vc7\lib" in my perl inc path.  The files I am trying to create is
> right out of perlXStut: Mytest.  The error I am getting is:
> 
> Microsoft (R) Program Maintenance Utility Version 7.10.3077
> Copyright (C) Microsoft Corporation.  All rights reserved.
> 

If you want to use Visual Studio .NET as your compiler you should build 
your perl with that compiler. ActiveState perl was built with Visual 
Studio 6 and that will lead to problems when you try to use Visual 
Studio .NET with it (as you have just found out). You could download the 
source to AS build 810 (perl 5.8.4) from the ActiveState site and build 
with Visual Studio .NET. It's not difficult. Or you would probably find 
that the latest source from CPAN (perl 5.8.5) also builds easily with 
Visual Studio .NET.

Alternatively, if you want to stick with ActiveState-built perl, then 
you should use Visual Studio 6 (MSVC++ 6.0) as your compiler.

There are possibly workarounds that enable you to use Visual Studio .NET 
with ActiveState-built perl, but I don't know what they are (and have 
little interest in learning of them :-)

Cheers,
Rob

-- 
To reply by email u have to take out the u in kalinaubears.



------------------------------

Date: Sat, 25 Sep 2004 12:27:22 GMT
From: Dave <dave@home.net>
Subject: Printing regex match
Message-Id: <MPG.1bbf040429fda709896bd@news.easynews.com>

I regularly use something along the lines of:

   my $string = 'some string to match';
   my @match = '';
   if ( @match = $string =~ /(pattern)/ ) {
      print "The match is\n@match\n";
   }
   else {
      print "Sorry, Dave. No match.\n";
   }

to test a regex by printing what matches.  However, if the regex becomes 
unwieldly and I assign it to a variable:

   my $regex = qr{pattern};
   my $string = 'some string';
   my @match = '';
   print "@match = $string =~ $regex\n"

I get a '1' returned instead of the matched bits.

If someone can let me know how to rewrite the above, I'd be grateful.

Thanks.





------------------------------

Date: Sat, 25 Sep 2004 09:02:04 -0400
From: Eric Amick <eric-amick@comcast.net>
Subject: Re: Printing regex match
Message-Id: <4vpal0dc3i53opoug7p0rs11o812qn64c7@4ax.com>

On Sat, 25 Sep 2004 12:27:22 GMT, Dave <dave@home.net> wrote:

>I regularly use something along the lines of:
>
>   my $string = 'some string to match';
>   my @match = '';
>   if ( @match = $string =~ /(pattern)/ ) {
>      print "The match is\n@match\n";
>   }
>   else {
>      print "Sorry, Dave. No match.\n";
>   }
>
>to test a regex by printing what matches.  However, if the regex becomes 
>unwieldly and I assign it to a variable:
>
>   my $regex = qr{pattern};
>   my $string = 'some string';
>   my @match = '';
>   print "@match = $string =~ $regex\n"
>
>I get a '1' returned instead of the matched bits.

What you've posted can't possibly be what you're using--Perl doesn't
interpolate arbitrary expressions in double quotes, at least not that
way. Assuming you meant

@match = $string =~ $regex;
print "@match\n";

the most likely explanation is that the pattern doesn't have any
parentheses in it. Using qr// won't magically parenthesize anything. If
all you want is what the entire pattern matches, you could use

@match = $string =~ /$regex/g;

-- 
Eric Amick
Columbia, MD


------------------------------

Date: Sat, 25 Sep 2004 15:06:38 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Printing regex match
Message-Id: <2rl8pfF1bk77fU1@uni-berlin.de>

Dave wrote:
> I regularly use something along the lines of:
> 
>    my $string = 'some string to match';
>    my @match = '';
>    if ( @match = $string =~ /(pattern)/ ) {
>       print "The match is\n@match\n";
>    }
>    else {
>       print "Sorry, Dave. No match.\n";
>    }
> 
> to test a regex by printing what matches.  However, if the regex
> becomes unwieldly and I assign it to a variable:
> 
> my $regex = qr{pattern};

<buggy code snipped>

> I get a '1' returned instead of the matched bits.
> 
> If someone can let me know how to rewrite the above, I'd be
> grateful.

Use capturing parentheses, either in the qr// operator, or do:

     @match = $string =~ /($regex)/

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


------------------------------

Date: 25 Sep 2004 13:07:54 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Printing regex match
Message-Id: <cj3qja$qhi$1@mamenchi.zrz.TU-Berlin.DE>

Dave  <dave@home.net> wrote in comp.lang.perl.misc:
> I regularly use something along the lines of:
> 
>    my $string = 'some string to match';
>    my @match = '';

That initialization is unnecessary and wrong.  It is unnecessary because
in the next statement you assign to @match again.  It is wrong because
it sets the first array element to an empty string.  That is not a state
that would make any sense if there was no other assignment following.

>    if ( @match = $string =~ /(pattern)/ ) {
>       print "The match is\n@match\n";
>    }
>    else {
>       print "Sorry, Dave. No match.\n";
>    }

As long as there is only a single string to be captured, a scalar
variable $match would be clearer:

    if ( my ( $match) = $string =~ /(pattern)/ ) {
        print "The match is\n$match\n";
    } # etc...

That would be for public(able) code.  For use as a debugging aid I'd
stick with the array, it works in more cases.

> to test a regex by printing what matches.  However, if the regex becomes 
> unwieldly and I assign it to a variable:
> 
>    my $regex = qr{pattern};
>    my $string = 'some string';
>    my @match = '';
>    print "@match = $string =~ $regex\n"
> 
> I get a '1' returned instead of the matched bits.

No, that would interpolate @match, $string, and $regex into the
output string.  It wouldn't do a regex match, or assign anything to
@match.

> If someone can let me know how to rewrite the above, I'd be grateful.

You need matching parentheses in the regex.  You can interpolate a
qr// in a literal regex like a string, so this would work:

    my @match = $string =~ /($regex)/

You could conceivably put the parentheses into the qr// itself, but
if this is only for debugging you don't want them there.

Quite generally it is a good idea to keep captures out of qr// expressions
(and strings that are to be interpolated in regexes) as far as possible.
To identify captures you have to count parentheses in the top-level regex.
That gets hard when parentheses are interpolated.

Anno


------------------------------

Date: Sat, 25 Sep 2004 14:31:11 +0800
From: Alont <end@dream.life>
Subject: what's wrong in this Perl Regex expression?
Message-Id: <41570a80.150555062@130.133.1.4>

$body is a html file waiting for process,
my purpose very simple----I want get the content of $body from
"<!--head-->" to "<!--bottom -->"

$body =~ {<!--head-->.*<!--bottom -->};
print $body;

the error record:
File::Glob object version 0.991 does not match bootstrap parameter
1.03 at F:/Perl/lib/XSLoader.pm line 95.

BTW: What's the string connect operator in Perl? for example:
there have $head, $body, $trailer and I want make them to one $
variable, if in VB I can worte: $all=$head & $body & trailer, I have
searched  perlop - Perl operators and precedence Document, find
nothing about this
-- 
      Your fault as a Government is My failure as a citizen


------------------------------

Date: Sat, 25 Sep 2004 08:53:28 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: what's wrong in this Perl Regex expression?
Message-Id: <pan.2004.09.25.06.53.28.144260@aursand.no>

On Sat, 25 Sep 2004 14:31:11 +0800, Alont wrote:
> $body is a html file waiting for process, my purpose very simple----I
> want get the content of $body from "<!--head-->" to "<!--bottom -->"
> 
> $body =~ {<!--head-->.*<!--bottom -->}; print $body;

Untested:

  if ( $body =~ /(<!--head-->.*<!--bottom-->)/ ) {
      my $content = $1;
  }
  else {
      # Is something wrong?
  }

If you don't want to use the 'if';

  my ( $content ) = $body =~ /.../;

> BTW: What's the string connect operator in Perl?

The '.' character.

> there have $head, $body, $trailer and I want make them to one $
> variable, if in VB I can worte: $all=$head & $body & trailer

Perl:

  my $all = $head . $body . $trailer;


-- 
Tore Aursand <tore@aursand.no>
"It's not so much what you have to learn if you accept weird theories,
 it's what you have to unlearn." (Isaac Asimov)


------------------------------

Date: Sat, 25 Sep 2004 17:28:48 +0800
From: Alont <end@dream.life>
Subject: Re: what's wrong in this Perl Regex expression?
Message-Id: <4159373d.162008078@130.133.1.4>

Tore Aursand <tore@aursand.no>Wrote at Sat, 25 Sep 2004 08:53:28
+0200:
>$body =~ /(<!--head-->.*<!--bottom-->)/ 

this expression always don't work in my Perl compiler,
and compiler don't give me error information, 
I've tried this:
$text_start = "<div id="boxtitle">";
$text_end = "<div id="bottom">";
my $content =~ $body =~ /($text_start.*$text_end)/;
this also no result.

-- 
      Your fault as a Government is My failure as a citizen


------------------------------

Date: Sat, 25 Sep 2004 09:49:16 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: what's wrong in this Perl Regex expression?
Message-Id: <wab5d.154323$XP3.144580@edtnps84>

Alont wrote:
> $body is a html file waiting for process,
> my purpose very simple----I want get the content of $body from
> "<!--head-->" to "<!--bottom -->"

Looks like it's time to read the Perl regular expressions tutorial.

perldoc perlretut


> $body =~ {<!--head-->.*<!--bottom -->};

If you want to delimit the match operator with '{' and '}' then you have to 
indicate that you are using the match operator by prepending an 'm' character 
or use the standard '/' match delimiters.

$body =~ m{<!--head-->.*<!--bottom -->};

$body =~ /<!--head-->.*<!--bottom -->/;


> print $body;

The match operator by itself does not modify the string it is bound to.  You 
probably want to use parentheses to capture the desired text.


> the error record:
> File::Glob object version 0.991 does not match bootstrap parameter
> 1.03 at F:/Perl/lib/XSLoader.pm line 95.
> 
> BTW: What's the string connect operator in Perl? for example:
> there have $head, $body, $trailer and I want make them to one $
> variable, if in VB I can worte: $all=$head & $body & trailer, I have
> searched  perlop - Perl operators and precedence Document, find
> nothing about this

TMTOWTDI

my $all = "$head$body$trailer";

my $all = $head . $body . $trailer;

my $all = join '', $head, $body, $trailer;

my $all = pack 'A*A*A*', $head, $body, $trailer;

my $all = sprintf '%s%s%s', $head, $body, $trailer;



John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Sat, 25 Sep 2004 09:59:20 +0000 (UTC)
From: mlush@hgmp.mrc.ac.uk (Mr. M.J. Lush)
Subject: Re: what's wrong in this Perl Regex expression?
Message-Id: <cj3fho$ea6$1@helium.hgmp.mrc.ac.uk>

In article <4159373d.162008078@130.133.1.4>, Alont  <end@dream.com> wrote:
>Tore Aursand <tore@aursand.no>Wrote at Sat, 25 Sep 2004 08:53:28
>+0200:
>>$body =~ /(<!--head-->.*<!--bottom-->)/ 
>
>this expression always don't work in my Perl compiler,
>and compiler don't give me error information, 
>I've tried this:
>$text_start = "<div id="boxtitle">";
>$text_end = "<div id="bottom">";
>my $content =~ $body =~ /($text_start.*$text_end)/;
>this also no result.

Converting to a proper program I get:-

#!/usr/bin/perl -w
use strict;

my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';

my $text_start = "<div id="boxtitle">";
my $text_end = "<div id="bottom">";
my $content =~ $body =~ /($text_start.*$text_end)/;
print $content;

Which produces errors, if it does not,  there is something wrong with
your computer!

To kick off you need to single quote the $text_start $text_end
so the quote marks in the string are not interpolated
and remove the ~ between $content and $body

#!/usr/bin/perl -w
use strict;

my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';
my $text_start = '<div id="boxtitle">';
my $text_end = '<div id="bottom">';
my $content = $body =~ /($text_start.*$text_end)/;
print $content;

the program now runs but prints '1' this is because 
$body =~ /($text_start.*$text_end)/; returns an list of
matches found in between the brackets.

my $content = 

puts the expression in a scalar context,  when an list is treated 
in a scalar context,  perl returns the number of elements in the list 
and puts that into the scalar. Since $body =~ /($text_start.*$text_end)/; 
can only make a single match $content gets the number 1.

To force perl to look at $content in a list context put brackets round
it.  In the same way you can declare an array @array = ("a", "b", "c");

#!/usr/bin/perl -w
use strict;

my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';

my $text_start = '<div id="boxtitle">';
my $text_end = '<div id="bottom">';
my ($content) = $body =~ /($text_start.*$text_end)/;
print $content;

for bounus points if you make the match global and replace $content
with an array.

my @content = $body =~ /($text_start.*$text_end)/g; 

5~@content gets _all_ instances of <div id="boxtitle">.*<div id="bottom">
in $body.

Hope this helps.


-- 
Michael
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NPC rights activist  |  Nameless Abominations are people too.


------------------------------

Date: Sat, 25 Sep 2004 12:54:24 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: what's wrong in this Perl Regex expression?
Message-Id: <pan.2004.09.25.10.54.23.859528@aursand.no>

On Sat, 25 Sep 2004 17:28:48 +0800, Alont wrote:
> I've tried this:
> $text_start = "<div id="boxtitle">";
> $text_end = "<div id="bottom">";
> my $content =~ $body =~ /($text_start.*$text_end)/;
> this also no result.

Of course, 'cause it doesn't even run.  Show us a script that actually
work, and then we'll help you.


-- 
Tore Aursand <tore@aursand.no>
"What we do is never understood, but only praised and blamed."
 (Friedrich Nietzsche)


------------------------------

Date: Sat, 25 Sep 2004 21:31:44 +0800
From: Alont <end@dream.life>
Subject: Re: what's wrong in this Perl Regex expression?
Message-Id: <415c7268.177154921@130.133.1.4>

mlush@hgmp.mrc.ac.uk (Mr. M.J. Lush)Wrote at Sat, 25 Sep 2004 09:59:20
+0000 (UTC):
>my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';
>
>my $text_start = '<div id="boxtitle">';
>my $text_end = '<div id="bottom">';
>my ($content) = $body =~ /($text_start.*$text_end)/;
>print $content;

maybe my html file includes some unexpected characters, My Perl
compiler can compile your script correctly but if I instead of $body
to My html file it won't work, how about your opinion?
-- 
      Your fault as a Government is My failure as a citizen


------------------------------

Date: Sat, 25 Sep 2004 14:23:28 +0000 (UTC)
From: mlush@hgmp.mrc.ac.uk (Mr. M.J. Lush)
Subject: Re: what's wrong in this Perl Regex expression?
Message-Id: <cj3v10$hvu$1@helium.hgmp.mrc.ac.uk>

In article <415c7268.177154921@130.133.1.4>, Alont  <end@dream.com> wrote:
>mlush@hgmp.mrc.ac.uk (Mr. M.J. Lush)Wrote at Sat, 25 Sep 2004 09:59:20
>+0000 (UTC):
>>my $body = '<div id="boxtitle">wibblewibblewibble<div id="bottom">';
>>
>>my $text_start = '<div id="boxtitle">';
>>my $text_end = '<div id="bottom">';
>>my ($content) = $body =~ /($text_start.*$text_end)/;
>>print $content;
>
>maybe my html file includes some unexpected characters, My Perl
>compiler can compile your script correctly but if I instead of $body
>to My html file it won't work, how about your opinion?

The unexpected character you are looking for is newline (aka \n)
the . in .* matches every character _except_ newline.

try this

my ($content) = $body =~ /($text_start.*$text_end)/s;

The reason why is in <http://www.perldoc.com/perl5.8.0/pod/perlretut.html>
-- 
Michael
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NPC rights activist  |  Nameless Abominations are people too.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 7176
***************************************


home help back first fref pref prev next nref lref last post