in reply to appending multiple values in an array

Perhaps you could share your code with us?

Replies are listed 'Best First'.
Re^2: appending multiple values in an array
by raghu_shekar (Novice) on Mar 18, 2009 at 14:51 UTC
    here s my code:
    foreach $file(@cad_err_list) { chomp($file); $err_loan_id1 = `grep 'DataDictionary' $file`; @err_loan_id2 = split(/<pciOFACViolation>/, $err_loan_id1); $reqd_1 = $err_loan_id2[0]; @err_loan_id3 = split(/\s/,$reqd_1); $reqd_2 = $err_loan_id3[2]; @err_loan_id = split(/>/,$reqd_2); }

    20090320 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

      see Markup in the Monastery before putting code
      the problem is that the data in the final array(result array) is getting over written and not appended. please help me out.... :(
      what you mean by final array here?
      one more hint, every time inside the forloop you are using so may intermediate variables and it will get overwritten.
      try printing those values inside the for loop and ensure that it prints properly, rather than seeing the values after the for loop.
      according to your code the variables $err_loan_id1,@err_loan_id2,$reqd_1,@err_loan_id3,$reqd_2,@err_loan_id will have the values that belong to the last file in the array @cad_err_list if you print the values at the end of for loop
      if you need only the final array then just have a hash declared at a global scope and maintain the intermediate variables like this.
      inside forloop @err_loan_id = split(/>/,$reqd_2); $err_loan_id{$file} = [@err_loan_id];
      now for every file the final array is saved, you can print at the end of for loop, try printing using Data::Dumper::Dumper you will recognize the output clearly now
      similarly if you want to save all the intermediate values do like this inside your for loop.
      $err_loan_id{$file} = [ $err_loan_id1,[ @err_loan_id2],$reqd_1,[@err_l +oan_id3],$reqd_2,[@err_loan_id]];
      I think I have answered your question..

      but you will end up in using more memory if you have so many files in @cad_err_list and because of that if the values in the intermediate variables are huge, then plainly using the methods shown by me is prone to problems, take care.


      Vivek
      -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
        Thanks a ton. i will try this and let you know how it went...