Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is a sub routine part of my code to process a zip file. It works, but the part I am having problems with is that inside of this zip file I have image files and a XML file that will have account numbers in it. I need to match the account numbers found inside of this XML file with the file name of these image files, the account number is part of the name on the image files - (account number=12345 - image file=12345_0223.jpg); and send results to this other sub routine to finish some other stuff. If you look at the code here I have the file name off the first foreach loop and I am pulling the account numbers from the second foreach loop from the XML tags"<account>(.*?)<\/account>". I am having a brain freeze trying to figure it out how to check if these values mach and how to send them to the next subroutine, I am stuck, any help would be great!
Thanks for looking!!!!
sub zip { my $zip_file = shift; return unless ($zip_file); my $zip = Archive::Zip->new( $zip_dir . $zip_file ) || die; my $file_name = $zip->read(); # read file xml foreach my $member ($zip->members()) { # print file information my $member_file_name = $member->fileName(); push @members_name, $member_file_name; # here I ahve the file na +me example: bunch of file names like: 12345_0223.jpg 77664_0223.jpg . +.. } my @xml_files = $zip->membersMatching( '.*\.XML' ); foreach (@xml_files) { my $xml = $_->xml(); open my $xml_fh, '<', \$xml or die "Can't open scalar filehandle: $!"; my $first_line = <$xml_fh>; while (<$xml_fh>) { chomp; if(/<account>(.*?)<\/account>/gi){ my $accountnumber = $1; print "Test - $accountnumber\n"; # here I have the account n +umbers from he xml file, like: 12345 77664 ... # now here I would like to match the account number, if its +part of a file name in @members_name # and send results to the other sub with corresponded values +. if () { to_sub( $zip, $accountnumber, $members_name[] ); } } } } }

Replies are listed 'Best First'.
Re: Match file names in a Zip file help
by jethro (Monsignor) on Feb 23, 2011 at 16:27 UTC

    If I understand you correctly, you need something like this:

    foreach (@members_name) { if (/^${accountnumber}_/) { to_sub($zip, $accountnumber, $_ ); } }

    If that is what you want, you could speedup this segment a bit if you stored the names in a hash:

    foreach my $member ($zip->members()) { # print file information my $member_file_name = $member->fileName(); my ($acc)= $member_file_name=~/^\d+/; push @{$members_name{$acc}}, $member_file_name; } ... if (exists $members_name{$accountnumber)) { foreach (@{$members_name{$accountnumber}) { to_sub( $zip, $accountnumber, $_ );
Re: Match file names in a Zip file help
by Gulliver (Monk) on Feb 23, 2011 at 18:09 UTC
    my @xml_files = $zip->membersMatching( '.*\.XML' ); foreach (@xml_files) { my $xml = $_->xml(); open my $xml_fh, '<', \$xml or die "Can't open scalar filehandle: $!";

    What is the $_->xml() part? I know $_ is a member but I can't find anything about this xml method in Archive::Zip.