gunzip module with arrays

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm looking for a code snippet that works to place an unzipped
contents of a file into an array.  I thought this should work but get
the following from the code:

$ ./
original: SCALAR(0x6055d30)
Reference_example ARRAY(0x5eaac90)
Array_example toast fruit cereal

# Code snippet
# gunzip example into an array reference
gunzip $input => \@outPut
   or die "gunzip failed: $GunzipError\n";

my @outPutArray = @$outPut;

foreach $outRec (@outPutArray) {

print "Referenced: $outPut\n";
print "Dereferenced: @$outPutArray\n";
print "original: @outPut\n";

#  Example of array reference that seems to work
my $array1_ref = ['toast', 'fruit', 'cereal'];
my @array = @$array1_ref;

print "Reference_example $array1_ref\n";
print "Array_example @array\n";

Re: gunzip module with arrays

  gldc> gunzip $input => \@outPut
  gldc>    or die "gunzip failed: $GunzipError\n";

  gldc> my @outPutArray = @$outPut;

@outPut is not the same as $outPut.

the former is an array which (supposedly) has the data, the latter is an
unrelated scalar with the same name and is empty. if you had run this
with strict and warnings you would have learned this.


Uri Guttman  ------  -------- --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- ---------
---------  Gourmet Hot Cocoa Mix  ---- ---------

Re: gunzip module with arrays

Quoted text here. Click to load it

Where does 'gunzip' come from? Please post *short* *complete* programs
we can all run. I assume you are loading IO::Uncompress::Gunzip.

Please also make sure your programs use 'strict' and 'warnings', and run
without errors or warnings. You have not declared any of the variables
you use above.

Why are you adding "\n" to the die message? Do you know what it does?

I'm not sure what you're trying to acheive by passing an arrayref to
gunzip. What it actually does is push a (single) scalar ref to the
gunzipped data onto the end of the array. Admittedly the documentation
is not entirely clear on this point.

If you want the output broken into lines, you will have to do this
yourself by splitting on $/.

Quoted text here. Click to load it

I think you need to (re-)read perldoc perlreftut. $outPut is not in any
way connected with @outPut, and @outPut is already an array.

If you had used 'strict', you would not only have got a complaint about
not having declared $outPut, but also about $outPut not containing an
array ref. Hopefully this would have lead you in the direction of
figuring out your problem on your own.

Quoted text here. Click to load it

Note the '$' on $array1_ref here. This is a scalar variable that is
assigned an array ref. This is not the same thing as an array variable,
which would begin with an '@'.


Site Timeline