Recently i've been working on a script that parses a file and formats the data and spits it out (and it's a finished piece because it e-mails the results as well ; ) One of the odd things about the data is that if there is a duplicate line we want to keep it and insert some previous data before that line. What i came up with is something like:

# in some loop looking through files if ($duplicate) { $data[$count]{VAl1} = $data[$count-1]{VAL1}; $data[$count]{VAL2} = $data[$count-1]{VAL2}; ... #flag data as being duplicate $data[$count]{VAL1} .= '/duplicate'; } $data[$count]{VALX} = /(par)se_so(me)_(dat)a/; next LOOP;
But the problem i have is that the previous data isn't getting sent into the array (of hashes) before the duplicate is sent into the array. When i run with the debugger it is putting the data into the array (and appending the duplicate flag), but apparently it's not being saved.

The reason i'm using $count to index @data is because i don't know which order the lines will go in, but i can check to make sure everything for one record has been pulled out (records aren't nested, thank whatever god you pray to : ) i never actually assign anything to an individual array spot, however, i'm always assigning to the hash inside the array spot. It's my understanding that this 'autovivifies' the spot in the array.

Since the changes are happening inside an if block my guess is that the newly autovivified entry is lost because it only gets lexical scope, maybe. But all of the data is getting set inside a while loop anyway, so why doesn't it all vanish after the while loop?

So the questions are (after that long intro):
a) am i correct that the changes are local even though the spot in the array is autovivified?
b) is there an elegant (or possibly kludgy-but-gets-job-done) way around this data being lost?

Please (un)confuse me,

jynx


In reply to local autovivification dismemberment... by jynx

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.