On Thu, Jul 20, 2006 at 10:51:58PM +0800, Sayed, Irfan (Irfan) wrote:
> Hi,
>  
> I have written following line. but i am getting error 
>  
> my @test  = ("/test" , "/playground");
> print @test;
> 
> i am getting output as follows
>  
> /test/playground
>  
> i need the output in following fasion
>  
> /test
> /playground
>  
> can you please tell me what is wrong?

In general, the problem is that you're printing two strings that don't
contain newline characters.  The newline escape character for an
interpolated string (surrounded by double quotes, for instance) is \n
(as indicated by Hal Wigoda's email).  One way to handle it, as
indicated by David Wagner, is to join the parts of the array into a
single string before using print() by way of the join() function with \n
as the "glue" character, thus inserting \n between each part of the
joined string.  This doesn't include a newline character at the end of
the new string, though, so if you are printing to STDOUT you might get
something like this:

  [EMAIL PROTECTED]:~$ ./script.pl
  /test
  /[EMAIL PROTECTED]:~$

What you probably want is something more like this:

  [EMAIL PROTECTED]:~$ ./script.pl
  /test
  /playground
  [EMAIL PROTECTED]:~$

You could produce that effect by adding an additional newline at the end
of your print statement, either via the concatenation operator or with a
comma to print a string containing nothing but the newline escape
character after the joined string.

Another option is to use the -l option in your shebang line, which
(under most circumstances) simply adds a newline to the end of every
print operation.  I find myself using -l more often than not these days,
in fact.  You could either combine the -l shebang line option with
join() to ensure that you get \n inserted in all the right places or use
the -l option by itself and rearrange the code slightly so that you're
not using join() at all:

  print $_ foreach @test;

This turns each array element into a separate scalar that is printed
individually, thus applying the effects of -l to each array element
rather than simply to the end of the array as with a line that reads
merely:

  print @test;

There are other ways to do it as well.  TIMTOWTDI.

relevant perldocs:

  perldoc perlop
  perldoc perlrun
  perldoc perltrap
  perldoc perlvar
  perldoc -f join
  perldoc -f print

-- 
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The measure on a man's real character is what he would do
if he knew he would never be found out." - Thomas McCauley

-- 
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