"..does not give me the entire elements in the array..it jus gives me the last hash in the array.."

Well, that's because you clobber the %alarm_details hash on each iteration.

Personally, I think you could do away with the @alarm_details array altogether and just use a HoH. Also, I'd go for a split rather than using a regexp, and split the lines directly into named variables. Something like this (untested):

my %alarm_details; my @files = </TELECOM_PROJ_MAIN/sas/tmp/t*>; foreach my $file (@files) { open LOG, "<", $path or die "Can't open $file:$!\n"; $alarm_details{filepath} = $file; while (<LOG>) { chomp; my ($key, $value) = split /=/, $_; $alarm_details{filepath}{$key} = $value if ($key); } close LOG; }

Also note that I changed @paths to @files, which seems to be more appropriate. And, the 3 argument form of open is better practise, as is checking $! upon failure.

One more thing to note is that the above code would throw "uninitialised value" warnings on any lines that had no data to the right-side of the equals (=). But you could get rid of that by explictitly checking for both $key and $value on each iteration.

Hope this helps,
Darren :)


In reply to Re: reuse hashes in another method by McDarren
in thread reuse hashes in another method by perumal

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.