Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
The stupid question is the question not asked
 
PerlMonks  

regex issue

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Hi monks , how do I do the following , the file I am reading contains :
A 33 B 22
I need to do the following :
my $A = "33"; my $B = "22";
thanks

Replies are listed 'Best First'.
output of dumper
by Anonymous Monk on Mar 23, 2004 at 16:44 UTC

    Edit by tye: Moved this previous version of the question from SoPW into this new thread.


    hi monks , I get this output
    ER 34 9YT 44 EE 66
    I need to do somthing like this
    my $ER = 34; my $9YT = 44; my $EE = 66;
    thanks

      What have you tried so far?

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

      First off $9YT isn't a valid valiable name. Variables can start with letters and underscores only. Using Data::Dumper should get almost what you want. There are no my's and i'm not sure how to get them, or if it is even possible with Data::Dumper.
      use strict; use warnings; use Data::Dumper; my $ER = 34; my $YT = 44; my $EE = 66; print Data::Dumper->Dump([$ER, $YT, $EE], [qw(ER YT EE)]); __OUTPUT__ $ER = 34; $YT = 44; $EE = 66;

      - Tom

        First off $9YT isn't a valid valiable name

        It is if you do enough tricks.

        $ perl -MData::Dumper -le '$f = "9YT"; $$f = "foo"; print Data::Dumper +::Dumper(\%main::)' $VAR1 = { # snip '9YT' => *{'::9YT'}, # snip };

        With symbol table lookups and symbolic refs, pretty much anything is possible in a Perl variable name. I don't know of a way to get lexicals to do this, though.

        ----
        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

      This seems like what you want:

      #! /usr/bin/perl use strict; use warnings; use Data::Dumper; my $str; do { local $/; $str = <DATA>; }; my $hash = { split /\s+/, $str }; print Dumper $hash; __DATA__ ER 34 9YT 44 EE 66

      _______________
      DamnDirtyApe
      Those who know that they are profound strive for clarity. Those who
      would like to seem profound to the crowd strive for obscurity.
                  --Friedrich Nietzsche
Re: regex issue
by Roy Johnson (Monsignor) on Mar 24, 2004 at 11: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 tcf22 (Priest) on Mar 24, 2004 at 11: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 davido (Cardinal) on Mar 24, 2004 at 11: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 tinita (Parson) on Mar 24, 2004 at 12:15 UTC
    i can't help, but that looks very similar to this question from about 19 hrs ago... what a coincidence...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://339458]
Approved by tye&nbsp;
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.