[Vienna-pm] Speichervariablen in Regulaeren Ausdruecken

Peter J. Holzer hjp-vienna-pm-list at hjp.at
Thu Jun 10 14:23:56 CDT 2004


On 2004-06-10 20:54:24 +0200, Christian 'arc' Schoeller wrote:
> Ich moechte mit einem Perl-Skript einzelne Teile der Ueberschrift
> eines HTML-Dokuments in unterschiedliche Variablen speichern.
> ,----[ extract.pl ]----
> | #!/usr/bin/perl
> |
> | open EXAMPLE, "example.html";
> | while (<EXAMPLE>) {     
> |   last if $_ =~ /<h1>\[(.+)\/(.+)\] (.+)<\/h1>/;
> |   }
> | close EXAMPLE;
> | print "$1, $2, $3";
>  \_____________________
> 
> Mein Problem ist, dass $1, $2 und $3 leer bleiben. Das Muster findet
> Uebereinstimmung, die Schleife wird an der richtigen Stelle verlassen,
> nur sind die Speichervariablen immer ohne Inhalt.
> 
> Was mache ich falsch?

perldoc perlvar:

|   $<digits>
|	   Contains the subpattern from the corresponding set of capturing
|	   parentheses from the last pattern match, not counting patterns
|	   matched in nested blocks that have been exited already.
|	   (Mnemonic: like \digits.)  These variables are all read-only
|	   and dynamically scoped to the current BLOCK.
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Die Die Variable $1, die Du nach dem Block ausliest, ist also nicht die
gleiche Variable $1, die im Block befüllt wird. Definiere Dir Deine
eigenen Variablen mit passendem Scope, um Werte dauerhaft zu speichern:

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

my ($t1, $t2, $t3);
open EXAMPLE, "example.html";
while (<EXAMPLE>) {     
    if (/<h1>\[(.+)\/(.+)\] (.+)<\/h1>/) {
	($t1, $t2, $t3) = ($1, $2, $3);
	last;
    }
}
close EXAMPLE;
print "$t1, $t2, $t3";

	hp

-- 
   _  | Peter J. Holzer    | I think we need two definitions:
|_|_) | Sysadmin WSR       | 1) The problem the *users* want us to solve
| |   | hjp at hjp.at         | 2) The problem our solution addresses.
__/   | http://www.hjp.at/ |    -- Phillip Hallam-Baker on spam
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/vienna-pm/attachments/20040610/6c97b48e/attachment.bin


More information about the Vienna-pm mailing list