append to the right instead of the bottom
am 10.09.2007 22:43:59 von 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
Re: append to the right instead of the bottom
am 10.09.2007 23:08:00 von xhoster
Jie wrote:
> 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.
Re: append to the right instead of the bottom
am 11.09.2007 00:09:37 von Gunnar Hjalmarsson
Jie wrote:
> 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
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl