in reply to Re: Read bzip2 directly into array
in thread Read bzip2 directly into array

@apple has nothing to be defined, it is used to store the content of the text from bz2. The content in $buffer will be pushed into @apple, as mentioned in the doc page. But I wonder why it just doesn't work.

Replies are listed 'Best First'.
Re^3: Read bzip2 directly into array
by baxy77bax (Deacon) on Apr 03, 2012 at 09:05 UTC
    use Data::Dumper; use strict ; use warnings ; use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ; use IO::File ; my $input = new IO::File "<tmp.bz2" or die "Cannot open 'tmp.bz2': + $!\n" ;; my $buffer; bunzip2 $input => \$buffer or die "bunzip2 failed: $Bunzip2Error\n +"; print $buffer; foreach ($buffer) { print Dumper($_);; }
    This works for me. Why do you need to have an @array defined (by the way i suppose it is defined somewhere else in the code!!). But even then i don't think it is a problem with bzip because if U are getting the output in print $buffer of print @{$buffer} then the problem must be somewhere else in the code. use Data::Dumper to check what is going on at each step in your foreach line.

      I would like to read each line of the text in .bz2 and pattern matching them with some keyword. But the problem with your code is the foreach loop only run once. That's why I thought want to store them in an array and use foreach to loop through all of the elements. But I don't know how to integrate the array as the output of the bzip2.