Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

appending multiple values in an array

by raghu_shekar (Novice)
on Mar 18, 2009 at 14:27 UTC ( #751450=perlquestion: print w/replies, xml ) Need Help??
raghu_shekar has asked for the wisdom of the Perl Monks concerning the following question:

hi, this might sound rather simple, i have an array of 5 elements, i pick each of the data from the array using the foreach loop, do some manupulations on the data and then store the end result into another array, the problem is that the data in the final array(result array) is getting over written and not appended. please help me out.... :(

Replies are listed 'Best First'.
Re: appending multiple values in an array
by jonnyfolk (Vicar) on Mar 18, 2009 at 14:45 UTC
    Well, if you're sure that the result is being overwritten, then you have probably declared the array within the foreach loop:
    my @array = (1,2,3,4,5); foreach my $line (@array) { my @newarray; push (@newarray, $line); print @newarray; }
    To prevent overwriting you should declare the new array outside the loop:
    my @array = (1,2,3,4,5); my @newarray; foreach my $line (@array) { push (@newarray, $line); } print @newarray;
    Of course, you could be doing something entirely different - so difficult to know without seeing the code...
Re: appending multiple values in an array
by toolic (Bishop) on Mar 18, 2009 at 14:34 UTC
    Help me help you by showing a running example of your code which demonstrates the problem. Are you pushing values into your new array?
      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_id32; @err_loan_id = split(/>/,$reqd_2); }
Re: appending multiple values in an array
by olus (Curate) on Mar 18, 2009 at 14:34 UTC

    If you show us what you are doing in your code, we could check what it is you are doing wrong and point you the right direction.

Re: appending multiple values in an array
by ig (Vicar) on Mar 19, 2009 at 08:29 UTC

    It is difficult to read your code the way you posted it. It will be much better if you surround your code in <code> tags when you post it. In the editor window it should look like this:

    <code> Your code here </code>

    Please do read Markup in the Monastery for more on this and other tags that you can use to format your posts. If your posts are better, you will get better responses.

    If I understand your question correctly, the following should help...

    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @err_loan_id; my @cad_err_list; foreach my $file (@cad_err_list) { chomp($file); my $err_loan_id1 = `grep 'DataDictionary' $file`; my @err_loan_id2 = split( /<pciOFACViolation>/, $err_loan_id1 ); my $reqd_1 = $err_loan_id2[0]; my @err_loan_id3 = split( /\s/, $reqd_1 ); my $reqd_2 = $err_loan_id3[2]; push(@err_loan_id, [ split( />/, $reqd_2 ) ] ); } print Dumper(\@err_loan_id);

    Note that I have used strict and warnings. You should add this to all your scripts - it will help you find many common bugs in your program. You can read Use strict and warnings to learn more about these very helpful options.

    I have changed the last line in your loop to push an anonymous array into the array @err_loan_id. This produces an Array of Arrays. Each element in the array @err_loan_id is a reference to an array containing the results of the final split(). When the loop is done you can access all your results.

    If this is new to you (I am guessing it is) then you should read perldata and perllol and perlref to understand how to create and access these more complex data structures. They are great and once you learn how to use them you will really appreciate them.

    I have added a print statement after the loop to print the data structure that is created in the loop. I have done this using Data::Dumper. This is a very helpful module for learning about complex data structures.

    update: fixed typo: s/err_load_id/err_loan_id/

      thanks a lot... this is very helpful..... appreciate all the help....
Re: appending multiple values in an array
by NateTut (Deacon) on Mar 18, 2009 at 14:34 UTC
    Perhaps you could share your code with us?
      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.
Re: appending multiple values in an array
by bichonfrise74 (Vicar) on Mar 18, 2009 at 16:22 UTC
    Maybe if you can give a sample data from the DataDictionary file and tell us what you are doing, then it would be easier to help you.
Re: appending multiple values in an array
by raghu_shekar (Novice) on Mar 18, 2009 at 14:54 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_id32; @err_loan_id = split(/>/,$reqd_2); }

      Run through Perl::Tidy and given <code> tags:

      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_id32; @err_loan_id = split( />/, $reqd_2 ); }

      If that's actually your code, I don't really see how it relates to your question. There isn't a push anywhere in here. There isn't anywhere that you're "adding" to an array. All your array operations are assignments, so naturally they're not appended.

      Usually to add to an array, we use push. In rare circumstances, unshift or splice.

      What do you expect this to do that it's not doing? Did you Use strict and warnings?

        im very new to perl, so if i store split( />/, $reqd_2 ) into a $variable and then give push ($variable, @array) inside the for loop, it should work??

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://751450]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2018-11-17 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (204 votes). Check out past polls.

    Notices?