454,280 Members | 1,601 Online Need help? Post your question and get tips & solutions from a community of 454,280 IT Pros & Developers. It's quick & easy.

# using push

 P: 72 hi... i have problem in using push function. Expand|Select|Wrap|Line Numbers @aa= qq (aa bb cc); push (@aa, 'dd'); print Dumper (@aa); exit; result as follow: \$VAR1 = 'aa bb cc'; \$VAR2 = 'dd'; actually wat i wan is \$VAR1 = 'aa bb cc dd'; i knew that i could archieve it through Expand|Select|Wrap|Line Numbers @aa= qq (aa bb cc); push (@aa, 'dd'); @aa=qq(@aa); print Dumper (@aa); exit; \$VAR1 = 'aa bb cc dd'; but i don wan to use the qq function.. is there any method to 'push' the new entry to the back of array and not the new line? Thanks Mar 14 '08 #1
12 Replies

 Expert 100+ P: 971 Use the qw operator. Expand|Select|Wrap|Line Numbers my @array = qw(aa bb cc); push @array, 'dd'; print Dumper(\@array); Expand|Select|Wrap|Line Numbers \$VAR1 = [           'aa',           'bb',           'cc',           'dd'         ]; --Kevin Mar 14 '08 #2

 P: 72 Use the qw operator. Expand|Select|Wrap|Line Numbers my @array = qw(aa bb cc); push @array, 'dd'; print Dumper(\@array); Expand|Select|Wrap|Line Numbers \$VAR1 = [           'aa',           'bb',           'cc',           'dd'         ]; --Kevin emm... not tat i wan. sorry as i didnt give a good example. the question is how to add an item to the end back of an array? looking at the push function, it will add to the new line of the array. please do tell me if my statement is not clear. thx Mar 14 '08 #3

 Expert 100+ P: 971 What I posted was adding the 'dd' to the end of the array. When you use the qq{} operator, it is simply a double quoted string which means that @array= qq{aa bb cc} has only one element 'aa bb cc', not an array with 3 elements. Expand|Select|Wrap|Line Numbers my @array = qw(aa bb cc); push @array,'dd';   print "First Element:  \$array \n"; # prints aa print "Second Element: \$array \n"; # prints bb print "Thrid Element:  \$array \n"; # prints cc print "Fourth Element: \$array \n"; # prints dd  which means that dd was added to the end or back of the array. Expand|Select|Wrap|Line Numbers my @array2 = qq{aa bb cc}; print \$array2; # returns aa bb cc which is the only element in the array. The code I posted above added the 'dd' to the end of the array and using the qw{} operator defines the array as three seperate elements. --Kevin Mar 14 '08 #4

 Expert 2.5K+ P: 4,059 push does add the new element to the end of the array, the problem is 'qq' as eWish has already noted. You should also a pass a reference to the data when you use Data::Dumper to print a data structure: print Dumper \@array; otherwise each element of the data structure is printed as a seperate VAR. Mar 14 '08 #5

 P: 72 thanks for inputs. I still confused. let take an example as below: Expand|Select|Wrap|Line Numbers @aa= ("Helo"); push(@aa, ",how are you");   print Dumper (@aa); exit; what i got is : \$VAR1 = 'Helo'; \$VAR2 = ',how are you'; instead i would like to get \$VAR1 = 'Helo,how are you''; Mar 14 '08 #6

 Expert 2.5K+ P: 4,059 Then use a string instead of an array. Mar 14 '08 #7

 Expert 2.5K+ P: 4,059 Also, Data::Dumper is what is causing the array to display like that, if you did not use Data::Dumper there would be no newlines in the display: Expand|Select|Wrap|Line Numbers @aa= ("Helo"); push(@aa, ",how are you"); print @aa; Mar 14 '08 #8

 P: 72 thanks for the input... i give another example: Expand|Select|Wrap|Line Numbers for \$j(1...10){ if (\$j== 5){ print "\$j\n"; } else { print "\$j\,"; } } exit; where i could get output as 1,2,3,4,5 6,7,8,9,10, because i would like to store the data to array first... Expand|Select|Wrap|Line Numbers for \$j(1...10){ if (\$j== 5){ #print "\$j\n"; push (@aa, \$j); } else { #print "\$j\,"; push (@aa, join(",",\$j,"")); } }   print Dumper (@aa); exit; where i get the output as \$VAR1 = '1,'; \$VAR2 = '2,'; \$VAR3 = '3,'; \$VAR4 = '4,'; \$VAR5 = 5; \$VAR6 = '6,'; \$VAR7 = '7,'; \$VAR8 = '8,'; \$VAR9 = '9,'; \$VAR10 = '10,'; __________________ i hope i could get \$VAR1 = '1,2,3,4,5'; \$VAR2 = '6,7,8,9,10,'; Mar 14 '08 #9

 Expert 100+ P: 410 For that, you need to push a newline into array along with the variable when its value becomes 5. Expand|Select|Wrap|Line Numbers for \$j(1...10){ if (\$j==5) { push (@aa, "\$j\n"); } else { push (@aa, join(",",\$j,"")); } }   print @aa;   Other than inserting newline after 5, if you want to have a newline after every multiple of 5 (5,10,15,...). Replace, Expand|Select|Wrap|Line Numbers if (\$j==5)   with: Expand|Select|Wrap|Line Numbers if (\$j%5==0)   Mar 14 '08 #10

 P: 72 For that, you need to push a newline into array along with the variable when its value becomes 5. Expand|Select|Wrap|Line Numbers for \$j(1...10){ if (\$j==5) { push (@aa, "\$j\n"); } else { push (@aa, join(",",\$j,"")); } }   print @aa;   Other than inserting newline after 5, if you want to have a newline after every multiple of 5 (5,10,15,...). Replace, Expand|Select|Wrap|Line Numbers if (\$j==5)   with: Expand|Select|Wrap|Line Numbers if (\$j%5==0)   emm... please refer to my post #9. Again. i wish to say that i want to get (using the print Dumper) \$VAR1 = '1,2,3,4,5'; \$VAR2 = '6,7,8,9,10'; thanks Mar 14 '08 #11

 Expert 100+ P: 410 If you are using Data::Dumper, each element of the array will be printed in new line. For your desired output, you should create an array with two elements "1,2,3,4,5" and "6,7,8,9,10". Try this: Expand|Select|Wrap|Line Numbers use Data::Dumper; \$str=""; for \$j(1...10){ if(\$j%5==0) { push @aa,\$str.\$j;      \$str="";  ###reinitialize to null } else { \$str.= \$j.",";   ## concatenate string with "," as delimiter } } print Dumper (@aa); exit;   Mar 14 '08 #12

 P: 72 If you are using Data::Dumper, each element of the array will be printed in new line. For your desired output, you should create an array with two elements "1,2,3,4,5" and "6,7,8,9,10". Try this: Expand|Select|Wrap|Line Numbers use Data::Dumper; \$str=""; for \$j(1...10){ if(\$j%5==0) { push @aa,\$str.\$j;      \$str="";  ###reinitialize to null } else { \$str.= \$j.",";   ## concatenate string with "," as delimiter } } print Dumper (@aa); exit;   thank you so much. Good day. thx Mar 14 '08 #13 