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

Re^2: Read bzip2 directly into array

by prescott2006 (Acolyte)
on Apr 03, 2012 at 11:22 UTC ( #963218=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^3: Read bzip2 directly into array
by Happy-the-monk (Canon) on Apr 03, 2012 at 11:45 UTC

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

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://963218]
[hippo]: That was around April 1994. Fun times.
[hippo]: They had purchased some shiny new 486 DXs the year before but crippled them by installing Win 3.1. I demo'd Linux with X on them to the department head and got the go-ahead to wipe Windows and install the new hotness.
[marto]: glory days ;)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2018-06-22 09:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.