in reply to Controlling the count in array
Hi EBK,
I am not 100% sure I understood your requirements, but try this out:
#! /usr/bin/env perl use strict; use warnings; use autodie; use 5.10.0; open my $fa, '<', 'file-a'; open my $fb, '<', 'file-b'; my %fa; my %fb; while (<$fb>) { chomp; my @F = split ','; push @{ $fb{"$F[2]:$F[1]"} }, $_; } while (<$fa>) { chomp; my @F = split ','; push @{ $fa{"$F[0]:$F[1]"} }, $_; } foreach my $key (keys %fa) { for my $i ( 0 .. $#{ $fa{$key} } ) { my @A = split ',', $fa{$key}[$i]; my @B = split ',', $fb{$key}[$i]; say join ',', $B[0], $B[1], $B[2], $A[2], $A[3], $A[4], $i + 1; } if ( @{ $fa{$key} } + 1 == @{ $fb{$key} } ) { my @A = split ',', $fa{$key}[-1]; my @B = split ',', $fb{$key}[-1]; say join ',', $B[0], $B[1], $B[2], $A[2], $A[3], $A[4], scalar @{ $fb{$key} }; } }
Output:
Unit4874,29031990,02,feb,21,5,1 Unit4875,29031990,02,feb,18,5,2 Unit4876,29031990,02,feb,20,5,3 Unit4877,29031990,02,feb,21,5,4 Unit4878,29031990,02,feb,21,5,5
Hope this helps,
Jim
Edit: As another monk pointed out in another of your questions, it seems like you could really benefit from some kind of SQL database. Seems like you want to do a join on two tables.
πάντων χρημάτων μέτρον έστιν άνθρωπος.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Controlling the count in array
by Veltro (Hermit) on Jun 08, 2018 at 12:25 UTC | |
|
Re^2: Controlling the count in array
by EBK (Sexton) on Jun 15, 2018 at 13:50 UTC | |
by jimpudar (Pilgrim) on Jun 15, 2018 at 17:40 UTC |