On 12/07/2011 08:44, Narasimha Madineedi wrote:
Hi everyone,

i have a file contains the following data.

A,1,B
A,2,B
B,3,C
B,1_1,A
A,2,D
C,3_3,B
B,2_2,A
D,2_2,A

for example A,1,B  its a request message ,corresponding response message
will be B,1_1,A somewhere in the file.

I have to sort the file in such a manner that after each request message
corresponding response message should follow immediatly.

can u help me how to resolve this issue?

Hi Narasimha

How are the response message tags paired with the requests? For
instance, how do we know in general whether the reply to A,1,B is
B,1_1,A or B,2_2,A?

If a request of the form X,99,Y always has a response of the form
Y,99_99,X, and and you say the response is always somewhere in the file,
then it is simplest just to ignore the responses and contruct them from
the requests as the are seen. The program below shows my point, but
somehow I think more than this is needed.

Cheers,

Rob


use strict;
use warnings;

while (my $rec = <DATA>) {

  chomp $rec;
  my ($src, $id, $dst) = split /,/, $rec;

  next if $id =~ /_/;               # Ignore responses
  print "$rec\n";
  print "$dst,${id}_${id},$src\n";  # Build response from request
}


__DATA__
A,1,B
A,2,B
B,3,C
B,1_1,A
A,2,D
C,3_3,B
B,2_2,A
D,2_2,A

**OUTPUT**

A,1,B
B,1_1,A
A,2,B
B,2_2,A
B,3,C
C,3_3,B
A,2,D
D,2_2,A



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to