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

Hello Monks,
I have the following data in a scalar variable $data. Here i want to split this scalar data by ^M^M and store the result in @splitdata array.
#!/usr/local/bin/perl my $data = 'Sending 104 bytes *** POST /path HTTP/1.0^M Content-type: text/xml^M Content-length: 37^M ^M SENT 104 HEADER HTTP/1.1 401 Unauthorised^M Connection: Close^M Content-Length: 0^M ^M <?xml version="1.0" encoding="UTF-8"?> <?path?> '; my @splitdata = split(/\n{2,}/, $data); my $lastValue = pop @splitdata print"Last value => $lastValue\n"; Expected output: Last value=> '<?xml version="1.0" encoding="UTF-8"?> <?path?>'
Can i get help from you please.

Replies are listed 'Best First'.
Re: how to split data by ^M^M
by moritz (Cardinal) on Jul 09, 2009 at 13:15 UTC
    I hope there are no literal ^Ms in your file, but actually carriage return characters that your editor displays as ^M.

    If that's the case you can split on \n\r\n\r. If not, you have to split on /\n\^M\n\^M/

    Your code doesn't work because there's a newline between the two ^M's.

      beg me pardon, but isn't it \r\n\r\n?

        Yes, you're right.
Re: how to split data by ^M^M
by BioLion (Curate) on Jul 09, 2009 at 13:14 UTC

    my @splitdata = split(/\^M/, $data); my $lastValue = pop @splitdata; print"Last value => $lastValue\n";

    prints

    Last value => <?xml version="1.0" encoding="UTF-8"?> <?path?>

    Just a something something...
      It works well on windows box. In my linux box it is not working.please help.

        I am on linux, works fine for me...
        can you give us more details (OS, perl version, more example code and output, any error messages you are getting in your linux machine, maybe dump out your @splitdata with Data::Dumper?).
        Also check out comments by moritz below...

        Just a something something...
Re: how to split data by ^M^M
by biohisham (Priest) on Jul 09, 2009 at 15:33 UTC
    You seem to be interested in only a certain output by popping the array you have created. What if you want the array @splitdata to be intact? or that your expected output WAS not the last element for you to be able to access it using pop() the way you did? and what if you did not want to split the $data into array elements?
    #!/usr/local/bin/perl use strict; use warnings; my $data = 'Sending 104 bytes *** POST /path HTTP/1.0^M Content-type: text/xml^M Content-length: 37^M ^M SENT 104 HEADER HTTP/1.1 401 Unauthorised^M Connection: Close^M Content-Length: 0^M ^M <?xml version="1.0" encoding="UTF-8"?> <?path?> '; #(Update) more like the output you requested print "Last Value=> "; while(){ (my $text = ($data=~ /(<\?.*\?>)/gi))? print qq('$1'\n) : exit +; } #OUTPUT: #Last Value=> '<?xml version="1.0" encoding="UTF-8"?>' #'<?path?>'
    the guys on Linux, try it too, I bet it can work fine in there as well....
    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind