a question about print in array

2005-05-24 Thread Frank

I met an interesting problem recently and am expecting your kind advice.

my input file (for_test)  is like as follows.   I wish add a   to the 
first line (before the word blue) and remove  at the last line.


# ---begining of the file, this line is not included in the file---#
blue
sky
skirt
sea
white
paper
flower
mine
red
face
flower
milk_white
milk
ivory
green
grand
tree

#___END,this line is not included in the file__#

my program is as this :

# to add the  to the first line and remove  at last line

#!/usr/bin/perl -w
use strict;

my $infile = for_test;

open (INPUT, $infile) || die  can not open $infile;
open OUTPUT, test.res ||die can not open test.res;

my @items = INPUT;

my $item = $items[0];
shift (@items);
unshift (@items, $item);
pop (@items);
chomp ($items[-1]);
print OUTPUT @items\n;

close OUTPUT;
close INPUT;
#___END_#



After execute the program.  The result was not as what I expected since 
the  add a new column at the left side. 

Iin the first line blue ,   is added by the program.  But it also 
add a new column to the file at the left side.  This is not what I 
expected becuase this also add a space to  other element at the left 
side when print it. 



blue
 sky
 skirt
 sea
 white
 paper
 flower
 mine
 red
 face
 flower
 milk_white
 milk
 ivory
 green
 grand
 tree





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




Re: a question about print in array

2005-05-24 Thread Jeff 'japhy' Pinyan

On May 24, Frank said:


print OUTPUT @items\n;


When you place an array inside quotes, it's the same as saying

  join($, @array)

where $ is the list separator variable, whose default value is   (a 
single space).


Thus, if @array is (this, that, those), then @array is this that 
those.  However, if @array is (this\n, that\n, those\n), then 
@array is this

 that
 those
.

Don't put the array in quotes when you're printing it to a file.

  print OUTPUT @items;

--
Jeff japhy Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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