Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use feature qw(say);
- use strict;
- use warnings;
- use experimental qw(signatures);
- {
- my @items = map { +{item => "item_" . $_} } 1..10;
- my @files; # all files
- my @items_not_found; # items whose associated file was not found
- my @idx_not_found; # indexes of items not found
- for my $i (0..$#items) {
- my $item = $items[$i];
- my $associated_file = search_associated_file( $item );
- if (defined $associated_file) {
- $files[$i] = $associated_file;
- } else {
- push @items_not_found, $item;
- push @idx_not_found, $i;
- }
- }
- # The program now invokes an external process to generate the associated files
- # for the missing items:
- @files[@idx_not_found] = generate_associated_files_of ( \@items_not_found);
- my $fn = 'associated_files.txt';
- open ( my $fh, '>', $fn ) or die "Could not open file '$fn': $!";
- for my $file (@files) {
- say $fh "$file";
- }
- close $fh;
- }
- sub generate_associated_files_of($items) {
- my @files;
- for my $item (@$items) {
- my ( $idx ) = $item->{item} =~ /item_(\d+)/;
- die "Unexpected index" if !defined $idx;
- push @files, "file_$idx";
- }
- return @files;
- }
- sub search_associated_file($item) {
- my ( $idx ) = $item->{item} =~ /item_(\d+)/;
- my %not_ok_idx = map { $_ => 1} 4,5,7;
- return undef if $not_ok_idx{$idx};
- return "file_" . $idx;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement