http://qs321.pair.com?node_id=339458

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: regex issue
by davido (Cardinal) on Mar 24, 2004 at 16:47 UTC
    You really should hold off on using symbolic references. <update>For one thing, symbolic refs can't be used to create lexical variables (which is what it appears you're trying to do).</update> Perl is one of a very few languages that even allow them, and yet look at how much work is getting accomplished with all those languages that don't implicitly allow symrefs.

    In the case of Perl, the best alternative to symbolic references, in many cases, is to use a hash. After all, the global symbol table itself is just a special kind of hash. If it's good enough for the goose, it's good enough for the gander.

    use strict; # Always, for now. use warnings; # Always, while developing code. my %symbols; open INFILE, "<mydata.txt" or die "Can't open infile.\n$!"; while ( my $key = <INFILE> ) { my $value; unless ( $value = <INFILE> ) { die "Uneven key/value pairs!"; } $value =~ s/^\s+|\s+$//g; $symbols{$key} = $value; } print "$_: $symbols{$_}\n" for keys %symbols;

    Also, please don't keep asking the same question with different titles. If you don't get good answers to your initial question, it's probably because it was unclear. In that case, you can follow-up in the same thread with refined details as to what you're looking for.


    Dave

Re: regex issue
by tcf22 (Priest) on Mar 24, 2004 at 16:40 UTC
    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

      <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
Re: regex issue
by Roy Johnson (Monsignor) on Mar 24, 2004 at 16:29 UTC
    What you're suggesting requires symbolic references. It's usually better to do it with a hash, where you'd end up with
    $hash{'A'} = '33'; $hash{'B'} = '22';

    The PerlMonk tr/// Advocate
      How about offering the questioner some (even untested) code?
      use strict; # always # The hash that we'll populate my %value_of; # You have to open the file and read it... my $file = "your file name"; open(FILE, "<", $file) or die "Cannot read '$file': $!"; my $last_variable; while (<FILE>) { # Remove the newline chomp; if (s/^\s+//) { # This was indented, assign it to the hash $value_of{$last_variable} = $_; } else { # This was not indented, it was a variable name $last_variable = $_; } } # %value_of now has the data.
        How about offering the questioner some (even untested) code?
        The question wasn't very detailed, so my answer wasn't very detailed. I don't think that answerers should put more into this than questioners.

        The PerlMonk tr/// Advocate
Re: regex issue
by tinita (Parson) on Mar 24, 2004 at 17:15 UTC
    i can't help, but that looks very similar to this question from about 19 hrs ago... what a coincidence...
A reply falls below the community's threshold of quality. You may see it by logging in.