rickerl has asked for the wisdom of the Perl Monks concerning the following question:

I am creating and writing data to a file using the Compress::Zlib module.

The code shows how I am using it. Each line of data is a string with no whitespace at the beginning with a return at the end.

my $gz = gzopen("$filename","wb"); $gz->gzwrite("@{$hash_name{$hash_data}}"); $gz->gzclose();
Sample output using "zcat myfile" would look something like this:
# Make Model Year ford mustang 2003 toyota landcruiser 1990 dodge caravan 1998
I don't know why the leading whitespace. Please help.

Thanks,
Ryan

Replies are listed 'Best First'.
Re: different output from gzwrite
by Paladin (Vicar) on Jul 12, 2003 at 04:04 UTC
    When interpolated into a string, and array has it's elements seperated by $", which by default is a space, so your string ends up looking like "ford mustang 2003\n toyota landcruiser 1990....". Try using
    { local $" = ""; $gz->gzwrite("@{$hash_name{$hash_data}}"); }
    which will set $" to the empty string, which will then be put between each element.
Re: different output from gzwrite
by jsprat (Curate) on Jul 12, 2003 at 04:07 UTC
    Just gzwrite @your_array;

    The reason is the same reason you get similar output if you print "@some_array"; when each element ends with a newline. Each element is separated with $" (also known as the list separator), which defaults to a space. Either leave out the quotes, locally change $" to '' or use something like join '', @your_array;

    For an in depth treatment see perlfaq5 and search for "weird spaces" - or at a prompt type perldoc -q "weird spaces"

    Good luck!

Re: different output from gzwrite
by bunnyman (Hermit) on Jul 14, 2003 at 14:35 UTC