[EMAIL PROTECTED] 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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to