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

Hello I am trying to truncate text between two hash keys and get all the strings in between those two strings. I have a function that can parse between two strings but when I try to parse between two strings that contain the '=>' characters I get an uninitialized variable error. Thank you monks for your endless wisdom :)

#Function call That works my $hashData = truncateText($inputText, "begin", "end"); #Desired function call that doesn't work my $hashData = truncateText($inputText, "begin =>", "end =>"); #Truncating Function sub truncateText{ #Truncates text by removing everything before and and after the pa +ssed in variables my($text, $beginString, $endString) = @_; my $truncatedText; if($text =~ /\b\Q$beginString\E\b(.*?)\b\Q$endString\E\b/s){ $truncatedText = $1; } return $truncatedText; }

Replies are listed 'Best First'.
Re: parsing to get data between '=>' characters
by choroba (Cardinal) on Apr 19, 2017 at 23:53 UTC
    There's no \b at the end of => . It matches at word boundaries, but > isn't a word character.

    /\b\Q$beginString\E\b(.*?)\b\Q$endString\E\b/s # ~~ ~~

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thanks! it worked!

Re: parsing to get data between '=>' characters
by duyet (Friar) on Apr 20, 2017 at 05:32 UTC

      Try Regexp::Debugger. It is one of the tools you will never regret knowing about (thanks, Damian!).