Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Controlling the count in array

by jimpudar (Pilgrim)
on Jun 08, 2018 at 03:16 UTC ( [id://1216166]=note: print w/replies, xml ) Need Help??


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

    Actually, I think that using a database would not be the best approach to solve this problem. The problem is where the OP says:

    ...If my last number of my counter(in this case is 4) minus the 5th column from File A is 1... and I have to get next line...

    In terms of relational databases it would mean one has to keep track of rows that have already been processed or 'next' rows or 'last occurrence' rows. Of course it can be done, but as far as I know it is going to be an ugly mess using row counters that each database technology handles differently.

    Besides that using relational database technologies the rule that the OP states: Table A.Column 5 - COUNTER == 1 does not give one row as result but five (marked with *):

    1,02 29031990 feb 21 5, 4,Unit4877 29031990 02 1 2,02 29031990 feb 18 5, 3,Unit4876 29031990 02 2 3,02 29031990 feb 20 5, 2,Unit4875 29031990 02 3 4,02 29031990 feb 21 5, 1,Unit4874 29031990 02 4 4,02 29031990 feb 21 5, 1,Unit4874 29031990 02 4 * 4,02 29031990 feb 21 5, 2,Unit4875 29031990 02 3 * 4,02 29031990 feb 21 5, 3,Unit4876 29031990 02 2 * 4,02 29031990 feb 21 5, 4,Unit4877 29031990 02 1 * 4,02 29031990 feb 21 5, 5,Unit4878 29031990 02 0 *

    The only way to solve that would be something like WHERE (TABLE A.COL5 - COUNTER_A = 1) AND (TABLE A.COL5 = COUNTER_B). Again that means using row counters and on top of that now I am making assumptions about the data and I actually don't want to go there.

    I have seen a couple of examples that EBK has posted and the only thing that I see is that he is being provided with very low quality data. I understand that he is trying to handle it the best way he can but I think that the solution should be coming from the organisation who are providing him this data.

    Here is some code:

Re^2: Controlling the count in array
by EBK (Sexton) on Jun 15, 2018 at 13:50 UTC
    Hi Jim, It's worked however some cases I got "Use of uninitialized value in join or string at" or "Use of uninitialized value in split at". in both files I have coincident matching keys and I can not have records like these:
    ,,,feb,21,5,5
    How can I solve this, a simple defined exists would solve this?

      You could add a check in each of the while loops at the start to validate the data is in the format you expect.

      If you include the portion of the inputs on which the program is failing, I might be able to help you more.

      Best,

      Jim

      πάντων χρημάτων μέτρον έστιν άνθρωπος.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1216166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found