hi...
i have problem in using push function. - @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 - @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
12 1891
Use the qw operator. - my @array = qw(aa bb cc);
-
push @array, 'dd';
-
print Dumper(\@array);
- $VAR1 = [
-
'aa',
-
'bb',
-
'cc',
-
'dd'
-
];
--Kevin
Use the qw operator. - my @array = qw(aa bb cc);
-
push @array, 'dd';
-
print Dumper(\@array);
- $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
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. - my @array = qw(aa bb cc);
-
push @array,'dd';
-
-
print "First Element: $array[0] \n"; # prints aa
-
print "Second Element: $array[1] \n"; # prints bb
-
print "Thrid Element: $array[2] \n"; # prints cc
-
print "Fourth Element: $array[3] \n"; # prints dd which means that dd was added to the end or back of the array.
-
my @array2 = qq{aa bb cc};
-
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
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.
thanks for inputs.
I still confused.
let take an example as below: - @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'';
Then use a string instead of an array.
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: - @aa= ("Helo");
-
push(@aa, ",how are you");
-
print @aa;
thanks for the input... i give another example: - 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... - 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,';
For that, you need to push a newline into array along with the variable when its value becomes 5. -
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,
with:
For that, you need to push a newline into array along with the variable when its value becomes 5. -
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,
with:
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
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: -
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;
-
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: -
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |