Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: regex issue

by tcf22 (Priest)
on Mar 24, 2004 at 16:40 UTC ( [id://339463]=note: print w/replies, xml ) Need Help??


in reply to regex issue

Using a hash would be better. Try this
my %data; while(<DATA>){ chomp; my $val = <DATA>; chomp($val); $data{$_} = $val; } use Data::Dumper; print Dumper \%data; __DATA__ A 33 B 22 __OUTPUT__ $VAR1 = { 'A' => ' 33', 'B' => ' 22' };

BUT if you really want to use symbolic reference(which I really advise against) you could use this.
while(<DATA>){ chomp; my $val = <DATA>; chomp($val); $$_ = $val; } print "A: $A\n"; print "B: $B\n"; __DATA__ A 33 B 22

- Tom

Replies are listed 'Best First'.
Re: Re: regex issue
by CountZero (Bishop) on Mar 24, 2004 at 20:12 UTC
    <nitpicking mode="on">

    ++ for the code with the symbolic references, but the OP specifically wanted to have lexical variables ("my"). Is it at all possible to have symbolic references to lexical variables?

    <nitpicking mode="off">

    CountZero

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

      No. As perlref says, Only package variables (globals, even if localized) are visible to symbolic references. Lexical variables (declared with my()) aren't in a symbol table, and thus are invisible to this mechanism.

      If you want to access lexical variables, you have to do a lot of mucking around with internals. Modules (like PadWalker) can get rid of most of that for you. But I don't think that this module will actually create new lexical variables in the previous scope. Perhaps some module will do it. If not, then I'd bet that it is possible for someone knowledgable to write such a beast.

      However actually using it would not be recommended...

      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
        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://339463]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found