Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Editing the contents of a variable

by jmcmillan1873 (Initiate)
on Aug 21, 2012 at 11:14 UTC ( #988698=perlquestion: print w/replies, xml ) Need Help??
jmcmillan1873 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I've just started to learn Perl and I'm trying to improve my knowledge.

I've written some code that will capture a string of text from between two points... specifically: <name> string-to-capture </name>
I just want to capture "string-to-capture" without the tags. The following code does that:
if ( $_ =~ m/<name>/ ) { $var1=$_; $var1 =~ /<name>(.*?)<\/name>/i; $var1 = $1; $var1 =~ s/ //g; print "$var1\n"; }
I imagine that there must be a better way of achieving this, could someone enlighten me please?

Replies are listed 'Best First'.
Re: Editing the contents of a variable
by ww (Bishop) on Aug 21, 2012 at 11:33 UTC

    Better? Who knows? That depends on your intent, your data, etc.

    More concise (and less reliance on $_... which is easy to overuse):

    #!/usr/bin/perl use 5.014; # 988698 my $str = "yada yada <name> string-to-capture </name> foo bar baz"; if ( $str =~ m!<name>(.*?)<\/name>! ) { my $var1=$1; $var1 =~ s/ //g; # removing spaces? print "$var1\n"; }

    However, if the actual "tags" are intended to be html, xml or suchlike, your "better way" almost certainly involves using an appropriate parser.


      Just a thought, if the 'string-to-capture' had information separated by spaces, your code would eliminate all spaces, maybe the special case:

      my $str = "yada yada <name> string-to-capture string-to-capture2 </n +ame> foo bar baz"; if ( $str =~ m!<name>(.*?)<\/name>! ) { my $var1=$1; $var1 = join(" ", split " ", $var1 ); # remove leading, trailin +g and # collapse internal white +space print "$var1\n"; }
      would be better. ( Taken from Camel book 3rd edition, page 154 )

      "Well done is better than well said." - Benjamin Franklin

        The s/ //g is OP's. Comment (question) is mine as, (for most purposes I can imagine in the background of the OP,) it makes no obvious sense.

        Your reply, however, calls attention to that, which is a good thing, if OP is cargo-culting or had missed the implications. + +

Re: Editing the contents of a variable
by Anonymous Monk on Aug 21, 2012 at 12:49 UTC
    #!/usr/bin/perl use 5.010; use strict; use warnings; for( "yada yada <name> string-to-capture </name> foo bar baz", 'another<name>string</name>' ){ my $var=join(q{},split(q{ },(m{<name>(.*?)</name>}is)[0]//next)); print "<$var>\n"; }
Re: Editing the contents of a variable
by jmcmillan1873 (Initiate) on Aug 21, 2012 at 14:54 UTC
    Thank you all for your feedback.

    In context: I'm trying to write a script that I can use to selectively remove data sources from XML dumps from an RRD file.
    I.e. When a datasource is obsolete and needs removed to avoid polluting the graph.

    This first section of the script is to get the names of the datasources. I'll now start the next part of my script, to push them to an array.

    Each of your answers is useful to help develop my own perl knowledge (and hopefully anyone else finding this thread.).
    Thanks again for your help.

    Regards -

      As ww hinted, using a parser to do the heavy lifting is better than hand rolling code to parse XML. The down side for someone starting out is typical modules (in this case XML::Twig is recommended) are pretty daunting. They look like they will take much longer to learn to use than hand rolling just the little bit of code that it seems you will need. In fact that is generally wrong. Especially for XML and HTML, unless you are generating the source files yourself, there are many subtleties and edge cases that will bite you.

      If you are writing this code to learn Perl then rolling your own parser will be very educational, but don't expect to get a reliable script written any time soon. If you are writing this to get a job done and learn some Perl along the way it is well worth spending the time to figure out how to use CPAN and (in this case) XML::Twig. Learning to use modules from CPAN is an important part of learning to use Perl effectively.

      True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988698]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-02-20 09:48 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (295 votes). Check out past polls.