On Thu, Jan 24, 2002 at 02:12:19PM +0100, Anette Seiler wrote:

> I have a text file named "fruit", containing several lines, like this:
> 
> apples
> oranges
> peaches
> apples
> bananas
> peaches
> bananas
> oranges
> 
> In my script I read the textfile into an array with the following code:
> 
>       open(INPUT, "fruit") or die "Can't open file ($!)\n";
>       @fruit = <INPUT>;
>       close INPUT;
> 
> This code does what I want; it reads each line as an element of the 
> array @fruit, except for one thing: it puts a space in front of each 
> element from the second element onwards. If I print @fruit I get:
> 
> apples
>  oranges
>  peaches
>  apples
>  bananas
>  peaches
>  bananas
>  oranges


You don't show the most relevant portion of the code, which is printing the
array.  I suspect it looks something like this:

print "@fruit";

When you interpolate an array inside a double-quoted string, the elements
are joined together with the value of the special variable $", i.e.:

"@fruit" eq join($", @fruit)

$" is space by default.  So, the spaces are not being added when you read
in the array.  In fact, there are no spaces in the array, which is why you
were unable to test for them or remove them.  The spaces are being added
when you print the array.

Try this instead:

print @fruit;


See perldata for more on strings in Perl.


Ronald

Reply via email to