in reply to Re: Re: regex issue
in thread regex issue

You would have to do it with eval:
use strict; use warnings; my $ref; my ($A,$B); while (<DATA>) { chomp; if (s/^\s+//) { eval "\$$ref = $_" } else {$ref = $_} } print "A=$A and B=$B\n"; __DATA__ A 33 B 22

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re^3: regex issue
by CountZero (Bishop) on Mar 24, 2004 at 22:04 UTC
    No, no, that's cheating.

    You cannot predeclare my ($A,$B) as you are not supposed to know what's in the data-file. It could be any variable name and any value.

    However, the eval suggestion is a good one. How about:

    $name ='A'; $value =10; $string = 'my $' . $name . '=' . $value . '; print "result inside eval +: $A\n";'; eval ($string) or die "ERROR: $@"; print "result outside eval: $A\n";

    As can be expected the lexical declaration does not survive outside of the eval though :-(

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law