Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Read bzip2 directly into array

by prescott2006 (Acolyte)
on Apr 03, 2012 at 07:58 UTC ( #963185=perlquestion: print w/ replies, xml ) Need Help??
prescott2006 has asked for the wisdom of the Perl Monks concerning the following question:

use strict ; use warnings ; use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ; use IO::File ; my $input = new IO::File "<file1.txt.bz2" or die "Cannot open 'fil +e1.txt.bz2': $!\n" ; my $buffer ; bunzip2 $input => \$buffer or die "bunzip2 failed: $Bunzip2Error\n +";
In the doc page (http://perldoc.perl.org/IO/Uncompress/Bunzip2.html#OO-Interface), it is stated that \$buffer in the sample code can be an array reference. So I try to write like this:
use strict ; use warnings ; use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ; use IO::File ; my $input = new IO::File "<file1.txt.bz2" or die "Cannot open 'fil +e1.txt.bz2': $!\n" ; my $buffer = \@apple ; bunzip2 $input => $buffer or die "bunzip2 failed: $Bunzip2Error\n" +; print @{$buffer}; foreach (@{$buffer}) { # do something }
But what I get is "Can't use an undefined value as an array reference..." and print out SCALAR {0x266a6ac}. Anybody can help me?

Comment on Read bzip2 directly into array
Select or Download Code
Re: Read bzip2 directly into array
by baxy77bax (Chaplain) on Apr 03, 2012 at 08:25 UTC
    The only thing i see here not working is the undefined @apple if i put anything in it i get the output. Use Data::Dumper package to see if the things you think are in the $buffer are really there and in the right place.

    cheers

    baxy

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

Re: Read bzip2 directly into array
by Happy-the-monk (Monsignor) on Apr 03, 2012 at 09:03 UTC

    You are not showing is the exact code you are using when you get to the named error message Can't use an undefined value as an array reference...

    The code you show will produce the following message: Global symbol "@apple" requires explicit package name at $0 line 6.

    Test with this: perl -e 'use strict; my $buffer = \@apple'

    So what is the code you are actually using?

    Cheers, Sören

      Sorry, I forgot to define the array when I type the code here.
      #!/usr/bin/perl 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 @apple; my $buffer = \@apple; bunzip2 $input => $buffer or die "bunzip2 failed: $Bunzip2Error\n +"; foreach (@{$buffer}) { if (/good(.*)/) { print $1; } }
      This is the code I wrote to read a txt contain in tmp.bz2 which contain: goodmorning goodbye goodafternoon and supposed to output: morning bye afternoon but it doesn't work.

        ... if (/good(.*)/)

        This is much better.
        Although $buffer now contains:

        $VAR1 = \[ \'goodmorning goodbye goodafternoon ' ];

        So although there's your array, it only contains scalar value referenced inside. I think that's not the behaviour you expected from the IO::Uncompress::Bunzip2 documentation talking about arrays.
        There it says:

        If $output is an array reference, the uncompressed data will be pushed onto the array.

        That is, all of the bunzipped data is pushed onto the array, so you can fill your array with data from several bunzipped files.

        Change the matching to if ($$_ =~ /good(.*)/) to make your code output the value.

        If you also want to break the result into a list, use split to do that.

        Cheers, Sören

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963185]
Approved by baxy77bax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2014-08-22 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (148 votes), past polls