I would like to substitute the hash name with

You don't have a hash name - which would mean you had named hashes - but hash keys in your array of anonymous hashes. If you change the key and reference that changed key, there's no value behind it - the value you are trying to acces is bound to the old (unchanged) key. To change a key, you have to copy over the value the old key is pointing at into the slot pointed at by the new key:

my $i = 1; $varname = "_address" . $i; $customer->[0]{$varname} = $customer->[0]{'_address'}; print "Customer Address", $customer->[0]{$varname};

But...

$customer->[0]{_address1} $customer->[0]{_address2}

whenever there are keys with a number attached, that cries for a revision of the data structure: store an anonymous array as the value to that hash. Then you have

$customer->[0]{_address}[0] $customer->[0]{_address}[1]

and looping becomes much easier:

my $arrayref = $customer->[0]{_address}; for my $address (@$arrayref) { # do whatever with $address ... }

But if you really have those column names (_address1,_addresss2,...,_adressn) in your tables, then the database design is flawed. You might want to normalize the addresses out to a another table.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: Hash name variable substitution by shmem
in thread Hash name variable substitution by peterb

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.