Andrej Kastrin am Montag, 23. Januar 2006 07.55:
> I wrote simple script, which have to concatenate multiple lines into
> array and then print each element of tihis array:
>
> I don't know where is the problem, Please, help!

The basic problem is that you try to print the result within the (while) loop 
that is building the result array. What your script does:

It shows the content of the array after every step of building it.

Besides that, there are other improvements - see inline:

# Alway start with:

use strict; # forces to declare variables
use warnings; # helpful for finding (potential) errors

> open INPUT,"<$ARGV[0]";

# missing error checking:
open (INPUT, '<', $ARGV[0]) or die "Can't open file: $!";

my @array;

> while ($line=<INPUT>){

while (my $line=<INPUT>){

>     push (@array,$line);

>     foreach $i(@array){
foreach my $i(@array){
>         print $i;
>    }

The above foreach loop should be outside of the while loop.

When you have to declare the variables (because of 'use strict'), you have to 
think about the scope of it (@array in this case). @array is defined in the 
same scope as the array building while loop. After the while, it is built, 
and you can use it. Basically, you have the following work:

- declare array
- fill array
- use array

> }
>
> Input is e.g.
> line 1
> line 2


hth,
joe

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


Reply via email to