OK, here is something I have never really been able to understand very well. Say I am running through a file, and I use a regex to match a certain condition:
while (<FILE>) { if (/.../) { ... } }
(The ... are not intended to be perl operators, they just mean 'something else goes here').
Easy enough. Now say that in the regex in the if(), we use parens to pull two strings, which are then assigned to $1 and $2, obviously. Now lets also say that as we run through this file, we use another regex to find a different pattern, which then matches another string ($1 again), which is then assigned to a global varible called, oh, '$stuff'. So here is the whole situation, pretty much:
my $stuff; while (<FILE>) { if (/(...)(...)/) { # we have $1 and $2 in here, both some string } elsif (/(...)/) { $stuff = $1; } }
(Obviously, this is all psuedo-code because my actual code is very messy and would not suffice for this discussion, but you get the idea.) Now here is where I get stuck. In the if() clause above (where $1 and $2 are assigned) I want to assign a hash value on the fly. Here is what I want to do (assume that anytime we are in the if(), $stuff has a value that was already assigned in the elsif() - in my code, it has to happen this way):
eval "\$$stuff{$1} = $2";
but as you can imagine, this does not in fact work. Now, I have never really been good at the whole reference thing and dynamic varible creation and such, so could someone please explain to me how I would do what I have illustrated above? I have a feeling that it is in fact a rather simple solution, but I continualy find myself up against a wall in this area!

Thanks so much,
R.Joseph

In reply to Creating on-the-fly hashes? by r.joseph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.