Hello,
I wanted to remove some common stuff from the text contained into some structures. I just came up with the attached code. However, is there anything simpler, maybe a search and substitute one-line instruction ?
Thank you.
use strict; use warnings; use Data::Dumper; my $var = '<Country>&lt;![CDATA[US]]&gt;</Country>'; $var = unwrap_cdata($var); print $var."\n"; my $hash = { america => '<Country>&lt;![CDATA[US]]&gt;</Country>', europe => ['<Country>&lt;![CDATA[IT]]&gt;</Country>','<Co +untry>&lt;![CDATA[UK]]&gt;</Country>'], }; print Dumper $hash; $hash = unwrap_cdata($hash); print Dumper $hash; sub unwrap_cdata { my $var = shift(); if (ref($var)) { $var = unwrap_hash($var) if (ref($var) eq 'HASH'); $var = unwrap_array($var) if (ref($var) eq 'ARRAY'); } else { $var = unwrap_scalar($var); } return $var; } sub unwrap_hash { my $href = shift(); foreach my $key (keys %{$href}) { my $hk = $href->{$key}; $hk = unwrap_hash($hk) if (ref($hk) eq 'HASH'); $hk = unwrap_array($hk) if (ref($hk) eq 'ARRAY'); $hk = unwrap_scalar($hk) if (not ref($hk)); $href->{$key} = $hk; } return $href; } sub unwrap_array { my $aref = shift(); my @array; my $i = -1; foreach my $av (@{$aref}) { $av = unwrap_hash($av) if (ref($av) eq 'HASH'); $av = unwrap_array($av) if (ref($av) eq 'ARRAY'); $av = unwrap_scalar($av) if (not ref($av)); push @array,$av; } return \@array; } sub unwrap_scalar { my $var = shift(); $var =~ s/<!\[CDATA\[//; $var =~ s/&lt;!\[CDATA\[//; $var =~ s/]]>//; $var =~ s/]]&gt;//; return $var; }

In reply to Search and substitute into data structures by rbi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.