Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

regex issue

by Anonymous Monk
on Mar 24, 2004 at 16:26 UTC ( #339458=perlquestion: print w/replies, xml ) Need Help??

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.


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


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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://339458]
Approved by tye&nbsp;
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-02-06 13:34 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (34 votes). Check out past polls.