Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk

I have received a message by using the code.
sub recvMessage { $MySocket->recv ($text,1000);#For Recv print $text; }
The print function shows as follows
INVITE tel:+1-212-555-2222 SIP/2.0 Via: SIP/2.0/UDP [5555::aaa:bbb:ccc:ddd]:1357;comp=sigcomp;branch=z9hG +4bKnashds7 Max-Forwards: 70 ............ ............
Now what i need is as follows
I want to store first line "INVITE tel:+1-212-555-2222 SIP/2.0" in a variable $req_line.

The "SIP/2.0/UDP [5555::aaa:bbb:ccc:ddd]:1357;comp=sigcomp;branch=z9hG4bKnashds7" part in a variable $via.(i.e. line started with Via:)

The part "70" in a variable $max_forwards (i.e line started with Max-Forwards:). and so on.

Plz suggest me a suitable code.

Regd's
Sanjay

Replies are listed 'Best First'.
Re: How to extract the different parts of a string stored in a varible?
by Corion (Patriarch) on Jul 07, 2008 at 10:48 UTC
Re: How to extract the different parts of a string stored in a varible?
by apl (Monsignor) on Jul 07, 2008 at 10:53 UTC
Re: How to extract the different parts of a string stored in a varible?
by Viki@Stag (Sexton) on Jul 07, 2008 at 11:07 UTC
    Now that $text is already defined & has value... following code can be used,
    my @txt_arr = split "\n",$text; my $header_hash{"req_line"} = shift @txt_arr; %header_hash = map { my $v = $_; (split /:/, $v)[0] => (split /:/,$v)[ +1] } @txt_arr;
    %header_hash will now have keys are the sip header's name & values are values of the sip header.
    http://techdiary-viki.blogspot.com/
      sorry abt the mistake... i had not noticed ':' in the header values. Consider this code ...
      my @txt_arr = split "\n",$text; chomp @txt_arr; my $header_hash{"req_line"} = shift @txt_arr; %header_hash = map { (/(.+?):/) => (/.+?:(.*)/) } @txt_arr;


      http://techdiary-viki.blogspot.com/