I have a file with records such as:
111112 4000000001 01/01/13 13:05
111112 4000000002 02/02/13 13:05
111112 4000000003 03/03/13 13:05
111113 4000000004 04/01/13 07:05
111113 4000000005 05/01/13 10:05
111113 4000000006 06/01/13 17:05
111114 4000000007 06/01/13 18:05
111114 4000000008 06/01/13 19:05
111114 4000000009 06/01/13 20:05
1st column is an customer id, 2nd column is an acct. no.
I would like it in this format:
111112,4000000001,4000000002,4000000003
111113,4000000005,4000000005,4000000006
111114,4000000007,4000000008,4000000009
here's my perl:
use diagnostics;
use strict;
use warnings;
my $line;
my $str;
my $prev="";
my $cur="";
my @accts=("");
open(INPUT, "sample.txt") || die("Error opening file for reading");
while($line = <INPUT>)
{
#print $line;
my @rec = split /\s+/, $line;
#$cur = $rec[1];
print "id: $rec[1]\n";
print "acct: $rec[2]\n";
print "admit_date: $rec[3]\n";
print "admit_time: $rec[4]\n";
if ( $rec[1] ne $prev )
{
$prev = $cur;
}
if( $rec[1] eq $prev )
{
push(@accts, $rec[2]);
print "@accts\n";
$cur = "";
}
print "\nprev: $prev\n";
}
print "\n\nScript finished!!!";
The concatenation does happen but it happens progressively unitl at the end of the while loop all accounts are concatenated instead of getting the correct output specified above. I know I'm close but my logic is off. Perhaps someone can point me in the right direction.
111112 4000000001 01/01/13 13:05
111112 4000000002 02/02/13 13:05
111112 4000000003 03/03/13 13:05
111113 4000000004 04/01/13 07:05
111113 4000000005 05/01/13 10:05
111113 4000000006 06/01/13 17:05
111114 4000000007 06/01/13 18:05
111114 4000000008 06/01/13 19:05
111114 4000000009 06/01/13 20:05
1st column is an customer id, 2nd column is an acct. no.
I would like it in this format:
111112,4000000001,4000000002,4000000003
111113,4000000005,4000000005,4000000006
111114,4000000007,4000000008,4000000009
here's my perl:
use diagnostics;
use strict;
use warnings;
my $line;
my $str;
my $prev="";
my $cur="";
my @accts=("");
open(INPUT, "sample.txt") || die("Error opening file for reading");
while($line = <INPUT>)
{
#print $line;
my @rec = split /\s+/, $line;
#$cur = $rec[1];
print "id: $rec[1]\n";
print "acct: $rec[2]\n";
print "admit_date: $rec[3]\n";
print "admit_time: $rec[4]\n";
if ( $rec[1] ne $prev )
{
$prev = $cur;
}
if( $rec[1] eq $prev )
{
push(@accts, $rec[2]);
print "@accts\n";
$cur = "";
}
print "\nprev: $prev\n";
}
print "\n\nScript finished!!!";
The concatenation does happen but it happens progressively unitl at the end of the while loop all accounts are concatenated instead of getting the correct output specified above. I know I'm close but my logic is off. Perhaps someone can point me in the right direction.