append to the right instead of the bottom

J

Jie

I am thinking if there is a way to append strings to the right in
perl.

for example, i want to create an output file like below:
A X
B Y
C Z

based on the following 2 arrays.
@array_1 = ("A", "B", "C");
@array_2 = ("X", "Y", "Z");

I am thinking to write some code like below:
#############################
@arrays = ("array_1", "array_2")
foreach $array (@arrays) {
open OUTPUT ">>final_file.txt";
$this_column ="";
foreach (@{$array}) {
$this_column .= $_;
}
print OUTPUT $this_column;
}
##############################

however, apparently, the above code will generates something like
below, instead of what i desired.... So, please help!!!! A temporary
2-dimentional array might not be a good idea though, because my real
data is really big....

A
B
C
X
Y
X
 
X

xhoster

Jie said:
I am thinking if there is a way to append strings to the right in
perl.

That is the only way to append strings. That is what append means. (Well,
assuming by "right" you mean when most people to when talking about
strings, the end.)

for example, i want to create an output file like below:
A X
B Y
C Z

based on the following 2 arrays.
@array_1 = ("A", "B", "C");
@array_2 = ("X", "Y", "Z");

I am thinking to write some code like below:
#############################
@arrays = ("array_1", "array_2")
foreach $array (@arrays) {
open OUTPUT ">>final_file.txt";

If you are going to post example code, please make sure it works
first. Unless syntax errors are what your example code is supposed
to be an example of.
$this_column ="";
foreach (@{$array}) {
$this_column .= $_;
}
print OUTPUT $this_column;
}
##############################

however, apparently, the above code will generates something like
below, instead of what i desired.... So, please help!!!! A temporary
2-dimentional array might not be a good idea though, because my real
data is really big....

You already have a 2-dimensional array, albeit one in which one of
the dimensions is in the symbol table. Your questions are more likely to
be taken seriously here if you follow the guide-lines by, for example,
using "strict" and thus using lexical variables rather than the symbol
table, and posting *real* code.

Anyway, the issues involved here seem to be morally equivalent to the "how
to tranpose a huge text file" you posted last month.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
G

Gunnar Hjalmarsson

Jie said:
i want to create an output file like below:
A X
B Y
C Z

based on the following 2 arrays.
@array_1 = ("A", "B", "C");
@array_2 = ("X", "Y", "Z");

while ( my $col_1 = shift @array_1 ) {
print "$col_1 ", shift @array_2, "\n";
}
I am thinking to write some code like below:
#############################
@arrays = ("array_1", "array_2")
foreach $array (@arrays) {
open OUTPUT ">>final_file.txt";
$this_column ="";
foreach (@{$array}) {
$this_column .= $_;
}
print OUTPUT $this_column;
}
##############################

however, apparently, the above code will generates something like

A
B
C
X
Y
X

Apparently?? On my computer, and leaving the syntax errors aside, your
code rather outputs

ABCXYZ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top