suggest me a perl script

suggest me a perl script

am 12.07.2011 09:44:50 von Narasimha Madineedi

--000e0ce00a5889bf4d04a7da79c2
Content-Type: text/plain; charset=ISO-8859-1

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?
--
Regards,
Narasimha Madineedi

--000e0ce00a5889bf4d04a7da79c2--

Re: suggest me a perl script

am 12.07.2011 10:08:42 von Jon Hermansen

--bcaec5431592e95dc904a7dace5c
Content-Type: text/plain; charset=UTF-8

Should be trivial to do in Perl (I won't provide code), but I always use sed
for jobs like these:

$ echo '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' | sed 'N; s/\n/: /'
>
> 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
>
>
On Tue, Jul 12, 2011 at 12:44 AM, 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?
> --
> Regards,
> Narasimha Madineedi
>

--bcaec5431592e95dc904a7dace5c--

Re: suggest me a perl script

am 12.07.2011 14:40:14 von Rob Dixon

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 = ) {

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/