-2

hello all i have a file xyz.txt having following info

69013 1-3039-1 REGISTER

69013 1-3039-1 100


69013 1-3039-1 401

69013 1-3039-2 REGISTER

69013 1-3039-2 100

69013 2-3039-1 REGISTER

69013 3-3039-1 REGISTER

69013 4-3039-1 REGISTER

.....................
.....................

69023 213-3039-2 REGISTER

69023 193-3039-2 100

69023 193-3039-2 401

69023 222-3039-1 REGISTER

69023 177-3039-2 100

69024 177-3039-2 401

69024 214-3039-1 100

69024 214-3039-1 401

69024 214-3039-2 REGISTER

where first coloumn is seconds

i want to store second coloumn for each different 3rd coloumn

codaddict
  • 445,704
  • 82
  • 492
  • 529
singhabsk
  • 31
  • 3

2 Answers2

1

Your requirement isn't very clear to me, but here is a script that stores second columns for each different 3rd column :

#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
use Data::Dumper;

my %result;
while (<DATA>) {
  chomp;
  next if /^\s*$/;
  my @cols = split;
  push @{$result{$cols[2]}}, $cols[1];
}
say Dumper \%result;


__DATA__
69013 1-3039-1 REGISTER

69013 1-3039-1 100


69013 1-3039-1 401

69013 1-3039-2 REGISTER

69013 1-3039-2 100

69013 2-3039-1 REGISTER

69013 3-3039-1 REGISTER

69013 4-3039-1 REGISTER

69023 213-3039-2 REGISTER

69023 193-3039-2 100

69023 193-3039-2 401

69023 222-3039-1 REGISTER

69023 177-3039-2 100

69024 177-3039-2 401

69024 214-3039-1 100

69024 214-3039-1 401

69024 214-3039-2 REGISTER

output:

$VAR1 = {
          '401' => [
                     '1-3039-1',
                     '193-3039-2',
                     '177-3039-2',
                     '214-3039-1'
                   ],
          'REGISTER' => [
                          '1-3039-1',
                          '1-3039-2',
                          '2-3039-1',
                          '3-3039-1',
                          '4-3039-1',
                          '213-3039-2',
                          '222-3039-1',
                          '214-3039-2'
                        ],
          '100' => [
                     '1-3039-1',
                     '1-3039-2',
                     '193-3039-2',
                     '177-3039-2',
                     '214-3039-1'
                   ]
        };
Toto
  • 89,455
  • 62
  • 89
  • 125
0

It does not sound to me like your question asks for what I think you want. So this is a solution to get all the values in column 2 cataloged by column 3. I apologize if this was not your intent.

open( my $fh, '<', '/path/to/xyz.txt' ) or die "Did not open $!";
my %hash;

while ( <$fh> ) { 
    next unless /\S/;
    my ( undef, $col2, $col3 ) = split ' ', $_, 3;
    next unless $col3;
    $hash{ $col3 }{ $col2 }++;
}

close $fh;

At this point, %hash will contain values of hashes that contain the hyphenated numbers in the second as keys.

This will print out the values.

foreach my $key ( sort keys %hash ) { 
    my $h = $hash{ $key };
    foreach my $k ( sort keys %$h ) { 
        print "$key, $k\n";
    }
}
Axeman
  • 29,660
  • 2
  • 47
  • 102