Home > Archive > PERL Beginners > February 2008 > Issue
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| mathkris@gmail.com 2008-02-11, 7:01 pm |
| Hi,
I am learning to print two arrays in a single file but unable to
do. So, I am printing it in two files. Any ideas
# Populating the arrays @alphaid and @betaid
foreach my $line (@File1)
{
if ($line =~ /^AC/)
{
$line =~ s/^AC\s*//;
@alphaid = $line;
push(@alphaid,$_);
}
if ($line =~ /^DR/)
{
$line =~ s/^DR\s*//;
@betaid = $line;
push(@betaid,$_);
}
}
#Printing the arrays in two files. Here I would like to have them in a
single with tab character separating the #arrays such as
# alphaid[1] \t betaid[1]
# alphaid[1] \t betaid[2]
# and so on
foreach $alphaid (@alphaid)
{
open (MYFILEG, '>>f1.txt');
print MYFILEG @alpha;
close (MYFILEG);
}
foreach $betaid (@betaid)
{
open (MYFILEE, '>>f2.txt');
print MYFILEE @betaid;
close (MYFILEE);
}
any help will be appreciated.
-K
| |
| Kashif Salman 2008-02-11, 7:01 pm |
| I am sure there are better ways as I am learning as well, but how
about doing something like this. This is assuming both arrays have the
same number of elements.
open MYFILE, ">>f1.txt";
for (0..$#alpha) {
print MYFILE "$alphaid[$_]\t";
print MYFILE "$betaid[$_]\n";
}
On Feb 11, 2008 12:11 PM, <mathkris@gmail.com> wrote:
> Hi,
> I am learning to print two arrays in a single file but unable to
> do. So, I am printing it in two files. Any ideas
>
> # Populating the arrays @alphaid and @betaid
> foreach my $line (@File1)
> {
> if ($line =~ /^AC/)
> {
> $line =~ s/^AC\s*//;
> @alphaid = $line;
> push(@alphaid,$_);
> }
>
> if ($line =~ /^DR/)
> {
> $line =~ s/^DR\s*//;
> @betaid = $line;
> push(@betaid,$_);
> }
> }
>
> #Printing the arrays in two files. Here I would like to have them in a
> single with tab character separating the #arrays such as
> # alphaid[1] \t betaid[1]
> # alphaid[1] \t betaid[2]
> # and so on
>
> foreach $alphaid (@alphaid)
> {
> open (MYFILEG, '>>f1.txt');
> print MYFILEG @alpha;
> close (MYFILEG);
> }
>
> foreach $betaid (@betaid)
> {
> open (MYFILEE, '>>f2.txt');
> print MYFILEE @betaid;
> close (MYFILEE);
> }
>
> any help will be appreciated.
> -K
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
| |
| Chas. Owens 2008-02-11, 7:01 pm |
| On Feb 11, 2008 3:11 PM, <mathkris@gmail.com> wrote:
> Hi,
> I am learning to print two arrays in a single file but unable to
> do. So, I am printing it in two files. Any ideas
>
> # Populating the arrays @alphaid and @betaid
> foreach my $line (@File1)
> {
> if ($line =~ /^AC/)
> {
> $line =~ s/^AC\s*//;
> @alphaid = $line;
> push(@alphaid,$_);
> }
>
> if ($line =~ /^DR/)
> {
> $line =~ s/^DR\s*//;
> @betaid = $line;
> push(@betaid,$_);
> }
> }
>
> #Printing the arrays in two files. Here I would like to have them in a
> single with tab character separating the #arrays such as
> # alphaid[1] \t betaid[1]
> # alphaid[1] \t betaid[2]
> # and so on
>
> foreach $alphaid (@alphaid)
> {
> open (MYFILEG, '>>f1.txt');
> print MYFILEG @alpha;
> close (MYFILEG);
> }
>
> foreach $betaid (@betaid)
> {
> open (MYFILEE, '>>f2.txt');
> print MYFILEE @betaid;
> close (MYFILEE);
> }
>
> any help will be appreciated.
> -K
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
If you want to print out only pairs you can use the code below. If
you want to print out all of the items in the the longer array, then
change the <= to >=
#!/usr/bin/perl
use strict;
use warnings;
my @a = qw<a b c d e>;
my @b = (1, 2, 3);
my $len = @a <= @b ? @a : @b;
for my $i (0 .. $len - 1) {
print "$a[$i]\t$b[$i]\n";
}
| |
| Gunnar Hjalmarsson 2008-02-11, 7:01 pm |
| Chas. Owens wrote:
> If you want to print out only pairs you can use the code below. If
> you want to print out all of the items in the the longer array, then
> change the <= to >=
In that case you'd better prevent Perl from spitting out uninitialized
warnings...
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @a = qw<a b c d e>;
> my @b = (1, 2, 3);
>
> my $len = @a <= @b ? @a : @b;
>
> for my $i (0 .. $len - 1) {
> print "$a[$i]\t$b[$i]\n";
> }
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| David Moreno 2008-02-11, 7:01 pm |
| Apparently, you are rewriting the value of the @alphaid and @betaid arrays
on each of the loops after the s///.
What are you trying to do with:
@alphaid = $line;
push(@alphaid,$_);
I'd try only:
push(@alphaid, $line);
if I understood correctly.
Cheers,
David.
On Feb 11, 2008 3:11 PM, <mathkris@gmail.com> wrote:
> Hi,
> I am learning to print two arrays in a single file but unable to
> do. So, I am printing it in two files. Any ideas
>
> # Populating the arrays @alphaid and @betaid
> foreach my $line (@File1)
> {
> if ($line =~ /^AC/)
> {
> $line =~ s/^AC\s*//;
> @alphaid = $line;
> push(@alphaid,$_);
> }
>
> if ($line =~ /^DR/)
> {
> $line =~ s/^DR\s*//;
> @betaid = $line;
> push(@betaid,$_);
> }
> }
>
> #Printing the arrays in two files. Here I would like to have them in a
> single with tab character separating the #arrays such as
> # alphaid[1] \t betaid[1]
> # alphaid[1] \t betaid[2]
> # and so on
>
> foreach $alphaid (@alphaid)
> {
> open (MYFILEG, '>>f1.txt');
> print MYFILEG @alpha;
> close (MYFILEG);
> }
>
> foreach $betaid (@betaid)
> {
> open (MYFILEE, '>>f2.txt');
> print MYFILEE @betaid;
> close (MYFILEE);
> }
>
> any help will be appreciated.
> -K
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--
David Moreno - http://www.damog.net/
Yes, you can.
| |
| John W. Krahn 2008-02-11, 10:05 pm |
| mathkris@gmail.com wrote:
> Hi,
Hello,
> I am learning to print two arrays in a single file but unable to
> do. So, I am printing it in two files. Any ideas
Instead of using two arrays just use one Array of Arrays.
> # Populating the arrays @alphaid and @betaid
> foreach my $line (@File1)
You should just read the filehandle in a while loop:
while ( my $line = <FILEHANDLE> ) {
It is more efficient than reading the whole file into an array.
> {
> if ($line =~ /^AC/)
> {
> $line =~ s/^AC\s*//;
Just do the test and substitution at the same time:
if ( $line =~ s/^AC\s*// )
> @alphaid = $line;
You are assigning a single element to @alphaid which means that @alphaid
will *always* have just *one* element at this point.
> push(@alphaid,$_);
You are appending the value of $_ to @alphaid which means that @alphaid
now has just *two* elements, although it is not clear from your code
that $_ contains a value.
> }
>
> if ($line =~ /^DR/)
> {
> $line =~ s/^DR\s*//;
> @betaid = $line;
> push(@betaid,$_);
Same as above.
> }
> }
>
> #Printing the arrays in two files. Here I would like to have them in a
> single with tab character separating the #arrays such as
> # alphaid[1] \t betaid[1]
> # alphaid[1] \t betaid[2]
> # and so on
Do you want to print both arrays to both files? Your code appears to be
printing nothing to either file?
>
> foreach $alphaid (@alphaid)
> {
> open (MYFILEG, '>>f1.txt');
> print MYFILEG @alpha;
> close (MYFILEG);
> }
>
> foreach $betaid (@betaid)
> {
> open (MYFILEE, '>>f2.txt');
> print MYFILEE @betaid;
> close (MYFILEE);
> }
Instead of opening and closing the files inside the loops you should do
that *outside* the loops. You should always verify that the file were
opened correctly as well.
To use an array of arrays to store your data you could do something like
this (UNTESTED):
my @data = [];
while ( my $line = <FILEHANDLE> ) {
if ( $line =~ s/^AC\s*// ) {
$data[ -1 ][ 0 ] = $line;
}
if ( $line =~ s/^DR\s*// ) {
$data[ -1 ][ 1 ] = $line;
}
if ( @{ $data[ -1 ] } == 2 ) {
push @data, [];
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
|
|
|