in reply to appending multiple values in an array

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Re: appending multiple values in an array

Replies are listed 'Best First'.
Re^2: appending multiple values in an array
by kyle (Abbot) on Mar 18, 2009 at 15:02 UTC

    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??

        If you store the result of split into a scalar variable like $variable, it will be the number of fields that would have been returned if you'd used an array instead. As such, you probably want to set "@variable = split />/, $reqd_2" instead.

        The documentation for push has a better explanation for how to use it than I could supply. For your simple example, I'd write "push @array, $variable".