http://www.perlmonks.org?node_id=984107


in reply to Re^3: needing to zip files in a series that begin with the same IP address
in thread needing to zip files in a series that begin with the same IP address

man im clueless on this i know how to regex for my file list just not sure how to apply to the code you gave.
#!perl use strict; use warnings; use Modern::Perl; use IO::Compress::Zip qw(zip $ZipError) ; my %files; while (<DATA>) { next unless /^(\d+.\d+.\d+.\d+)/; push @{ $files{$1} }, $_; } for my $ip ( keys %files ) { do { print "Zip $ip -> $_" } for @{ $files{$ip} }; } __DATA__ my @files = <*.txt *.docx>;
  • Comment on Re^4: needing to zip files in a series that begin with the same IP address
  • Download Code

Replies are listed 'Best First'.
Re^5: needing to zip files in a series that begin with the same IP address
by Kenosis (Priest) on Jul 27, 2012 at 22:07 UTC

    Hi, diamondsandperls.

    You're certainly not clueless, as you've mostly done this work yourself.

    Sorry, but I didn't mean to confuse the issue by using __DATA__, but it was for example only (gives a source of the data for the script). So, you can use what you've already done like this:

    use Modern::Perl; use IO::Compress::Zip qw(zip $ZipError) ; my %files; my @files = <*.txt *.docx>; for (@files) { next unless /^(\d+.\d+.\d+.\d+)/; push @{ $files{$1} }, $_; } for my $ip ( keys %files ) { do { print "Zip $ip -> $_" } for @{ $files{$ip} }; }

    Place your zipping call within the do{} block (replacing the printing placeholder), noting that $ip contains the common IP address and $_ contains the complete file name to be zipped.

    ps Modern::Perl contains both strict and warnings.

      Thanks for your help. I am having trouble nailing that part just right i have tried several zip calls but the code is failing. Thanks for the heads up on Modern::Perl I was curious of what the module was doing.

      here is what seems to be the best educated guess

      #!perl use Modern::Perl; use IO::Compress::Zip qw(zip $ZipError) ; my %files; my @files = <*.txt *.docx>; for (@files) { next unless /^(\d+.\d+.\d+.\d+)/; push @{ $files{$1} }, $_; } for my $ip ( keys %files ) { my $output = "$ip.zip"; do { zip => $output, $ip -> $_ } for @{ $files{$ip} }; }

        Remember to check for any return errors when zipping. Also, you can pass an array reference to zip, so try the following:

        for my $ip ( keys %files ) { zip \@{ $files{$ip} } => "$ip.zip" or die "zip failed: $ZipError\n +"; }