Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenate based on 1st column in a file of records

Status
Not open for further replies.

Haazi2

Programmer
Aug 25, 2000
51
US
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.
 
From your example it is unclear what do you want for something like
Code:
111112 4000000001 01/01/13 13:05
111112 4000000002 02/02/13 13:05
111113 4000000003 04/01/13 07:05 
111113 4000000004 05/01/13 10:05 
111112 4000000005 03/03/13 13: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
Do you need to collect all the lines for the same id or only the consecutive ones?
In the first case you need to read the whole file into a hash:
Code:
while(<INPUT>){
  @rec=split;
  push @{$myhash{$rec[0]}},$rec[1];
}
for(sort keys %myhash){
  print join(',',@{$myhash{$_}}),"\n";
}

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top