473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

using push

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
12 1891
eWish
971 Expert 512MB
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
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
971 Expert 512MB
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
Then use a string instead of an array.
Mar 14 '08 #7
KevinADC
4,059 Expert 2GB
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
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
410 Expert 256MB
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
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
410 Expert 256MB
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
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

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

Similar topics

14
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
3
by: i | last post by:
Hi, I'm working with an array of nodes, numbering roughly in the thousands. Each node has at least one, but up to four, references to another node - North, South, East, or West. I'm trying to...
8
by: dbaplusplus | last post by:
I worked on web development using java script many many years, so I am now a newbie to javascript. I have a single html page, which is generated dynamically using some programming language. Web...
34
by: John | last post by:
This produces an initialized array to zero: int *i = new int() ; 004124B0 push ebp 004124B1 mov ebp,esp 004124B3 mov eax,dword ptr 004124B6 push eax...
2
by: hakimks | last post by:
You are provided with a sample C programs: calc.c, which implements a reverse polish notation calculator. Study it carefully. This program uses a stack (of course!) but the stack implementation is...
9
by: Allen | last post by:
The arguments of function is variable. Given function address, argument type and data, how to dynamically call the function? The following is pseudo code. int count = 0; int offset = 0; char...
1
by: bsprogs | last post by:
I am currnetly programming a file hosting website in PHP and I am slowly integrating AJAX into the website. Here is my problem: The user uploads the file. The server processes the file and...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.