If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Reading columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-03-07, 18:03
asram asram is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
Reading columns

Hi,
I have a file with the following data:
a:1:m
b:2:n
c:1:h
d:2:k
f:2:r

I want the output to be based on the matching of the second column:

1
m,h

2
n,k,r

I am trying this way, but not getting there:
#!/usr/bin/perl
my @second;
$file="a.txt";
open (DAT, $file) || die ("Could not open file $file");
$i=0;
for $line (<DAT>) {
my ($a,$b,$c)=split(/:/,$line);
chomp $c;
push (@ram, "$c");
%ba = ("$b", @second);
}
while (($key,$value) = each (%ba)) {
print $key.",".$value;
print $key;
}
print %ba;


Any help will be greatly appreciated
Reply With Quote
  #2 (permalink)  
Old 03-04-07, 00:58
KevinADC KevinADC is offline
Registered User
 
Join Date: Feb 2006
Posts: 56
one way using a hash of arrays:

Code:
use strict;
use warnings;
my $file="a.txt";
my %hash = ();
open (DAT, $file) || die ("Could not open file $file");
while (my $line = <DAT>) {
   chomp($line);
   my ($v1,$v2) = (split(/:/,$line))[1,2];
   push @{$hash{$v1}},$v2;
}
close(DAT);
foreach my $key (sort {$a <=> $b} keys %hash) {
   print "$key\n",join(',',@{$hash{$key}}),"\n\n";
}
Reply With Quote
  #3 (permalink)  
Old 03-04-07, 21:12
asram asram is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
This worked. Thanks much! Great Help!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On