By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,280 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
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
  1. @aa= qq (aa bb cc);
  2. push (@aa, 'dd');
  3. print Dumper (@aa);
  4. 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
  1. @aa= qq (aa bb cc);
  2. push (@aa, 'dd');
  3. @aa=qq(@aa);
  4. print Dumper (@aa);
  5. 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
Share this Question
Share on Google+
12 Replies


eWish
Expert 100+
P: 971
Use the qw operator.

Expand|Select|Wrap|Line Numbers
  1. my @array = qw(aa bb cc);
  2. push @array, 'dd';
  3. print Dumper(\@array);
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = [
  2.           'aa',
  3.           'bb',
  4.           'cc',
  5.           'dd'
  6.         ];
--Kevin
Mar 14 '08 #2

P: 72
Use the qw operator.

Expand|Select|Wrap|Line Numbers
  1. my @array = qw(aa bb cc);
  2. push @array, 'dd';
  3. print Dumper(\@array);
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = [
  2.           'aa',
  3.           'bb',
  4.           'cc',
  5.           'dd'
  6.         ];
--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

eWish
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
  1. my @array = qw(aa bb cc);
  2. push @array,'dd';
  3.  
  4. print "First Element:  $array[0] \n"; # prints aa
  5. print "Second Element: $array[1] \n"; # prints bb
  6. print "Thrid Element:  $array[2] \n"; # prints cc
  7. print "Fourth Element: $array[3] \n"; # prints dd  which means that dd was added to the end or back of the array.
Expand|Select|Wrap|Line Numbers
  1. my @array2 = qq{aa bb cc};
  2. print $array2[0]; # 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

KevinADC
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
  1. @aa= ("Helo");
  2. push(@aa, ",how are you");
  3.  
  4. print Dumper (@aa);
  5. 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

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

KevinADC
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
  1. @aa= ("Helo");
  2. push(@aa, ",how are you");
  3. print @aa;
Mar 14 '08 #8

P: 72
thanks for the input... i give another example:

Expand|Select|Wrap|Line Numbers
  1. for $j(1...10){
  2. if ($j== 5){
  3. print "$j\n";
  4. }
  5. else {
  6. print "$j\,";
  7. }
  8. }
  9. 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
  1. for $j(1...10){
  2. if ($j== 5){
  3. #print "$j\n";
  4. push (@aa, $j);
  5. }
  6. else {
  7. #print "$j\,";
  8. push (@aa, join(",",$j,""));
  9. }
  10. }
  11.  
  12. print Dumper (@aa);
  13. 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

nithinpes
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
  1. for $j(1...10){
  2. if ($j==5)
  3. {
  4. push (@aa, "$j\n");
  5. }
  6. else {
  7. push (@aa, join(",",$j,""));
  8. }
  9. }
  10.  
  11. print @aa;
  12.  
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
  1. if ($j==5)
  2.  
with:

Expand|Select|Wrap|Line Numbers
  1. if ($j%5==0)
  2.  
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
  1. for $j(1...10){
  2. if ($j==5)
  3. {
  4. push (@aa, "$j\n");
  5. }
  6. else {
  7. push (@aa, join(",",$j,""));
  8. }
  9. }
  10.  
  11. print @aa;
  12.  
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
  1. if ($j==5)
  2.  
with:

Expand|Select|Wrap|Line Numbers
  1. if ($j%5==0)
  2.  

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

nithinpes
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
  1. use Data::Dumper;
  2. $str="";
  3. for $j(1...10){
  4. if($j%5==0)
  5. { push @aa,$str.$j; 
  6.     $str="";  ###reinitialize to null
  7. }
  8. else {
  9. $str.= $j.",";   ## concatenate string with "," as delimiter
  10. }
  11. }
  12. print Dumper (@aa);
  13. exit;
  14.  
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
  1. use Data::Dumper;
  2. $str="";
  3. for $j(1...10){
  4. if($j%5==0)
  5. { push @aa,$str.$j; 
  6.     $str="";  ###reinitialize to null
  7. }
  8. else {
  9. $str.= $j.",";   ## concatenate string with "," as delimiter
  10. }
  11. }
  12. print Dumper (@aa);
  13. exit;
  14.  
thank you so much. Good day.
thx
Mar 14 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.