Gossamer Forum
Home : General : Perl Programming :

array -- annoying space

Quote Reply
array -- annoying space
Hi all

In the following code I find an annoying space
which appears at the start of the 2nd and each
subsequent line both in the arrayfile.txt and the displayed
output of the array.


Does anyone know what is going on and how to
avoid this problem.


#!/bin/perl


$localtime= localtime;
#---------------------------------
$line0="$localtime|this is line 0\n";
$line1="$localtime|this is line number 1\n";
$line2="$localtime|this is line No 2\n";
@array=();
push (@array,$line0,$line1,$line2,);
print @array;
print $localtime;
#---------------------------------
$fname = "F:/webtest/test/cgi-bin/help3/arrayfile.txt";
open (ARRAYFILE, ">$fname");
print ARRAYFILE "@array\n";
close (ARRAYFILE);
Quote Reply
Re: [courierb] array -- annoying space In reply to
Not sure, but I would use;

print ARRAYFILE join('',@array) . "\n";

Not sure that "@array" would do quite what you are wanting.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [courierb] array -- annoying space In reply to
It's pretty much what Andy mentioned. When you wrap the array in double quotes it gets interpolated before print sees it. When interpolated like that the elements are joined via the value of the $, Perl special var which is a space. [edit]Unless of course it gets changed somewhere along the way.[/edit]

This should be all you need:

Code:
print ARRAYFILE @array;

You already have new lines appending to the end of each element.

Here's some more info: http://www.perl.com/...06/18/variables.html

~Charlie

Last edited by:

Chaz: Aug 21, 2004, 7:15 PM
Quote Reply
Re: [courierb] array -- annoying space In reply to
Hey courieb,

Chaz has it right. However, this being Perl Wink.... here's how I would do it:

  1. remove the \n from the end of each of your strings (unless you have a requirement to have them there)
  2. change the local value of $, (or $" depending on what you are printing) to \n
For example:
Code:
#!/bin/perl

$localtime= localtime;
#---------------------------------
$line0="$localtime|this is line 0";
$line1="$localtime|this is line number 1";
$line2="$localtime|this is line No 2";
@array=();
push @array,$line0,$line1,$line2;
{
local $, = "\n";
print @array, '';
}

print $localtime;
#---------------------------------
$fname = "F:/webtest/test/cgi-bin/arrayfile.txt";
open (ARRAYFILE, ">$fname");
{
local $, = "\n";
print ARRAYFILE @array;
}

close (ARRAYFILE);
This will add a newline between each item in the print command.
I added the , '' to the first print so that it would add a newline after the last item of @array. However I could have also done:
Code:
...
print @array;
}
print "\n", $localtime;
...
Which is probably better.

Just a note: If you print @array inside of double quotes, you would need to change the local value of $" instead of $,
like so:
Code:
{
local $" = "\n";
print "@array";
}
print "\n", $localtime;

Hope this helps.
Quote Reply
Re: [Crolguvar] array -- annoying space In reply to
Thanks all. i will have a try.
Quote Reply
Re: [Crolguvar] array -- annoying space In reply to
let us say
$line0="$localtime|this is line 0+";
$line1="$localtime|this is line number 1+";
$line2="$localtime|this is line No 2+";

what if i want insert the @array to a table.
so the value will be

$localtime|this is line 0+ $localtime|this is line number 1+ $line2="$localtime|this is line No 2+

will it be posssible to remove the space after each "+" only

$localtime|this is line 0+$localtime|this is line number 1+$line2="$localtime|this is line No 2+

Thanks

Last edited by:

courierb: Sep 12, 2004, 1:12 AM
Quote Reply
Re: [courierb] array -- annoying space In reply to
HI Crolguvar .

i know how to do it now.

thanks