Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How can one re-parse(?) a string to fill in variables

by Anonymous Monk
on Nov 23, 2000 at 22:43 UTC ( #43151=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Comment on How can one re-parse(?) a string to fill in variables
Re: How can one re-parse(?) a string to fill in variables
by merlyn (Sage) on Nov 23, 2000 at 22:45 UTC
    How can one phrase a question so as to make it answerable? {grin}

    One possible solution, since the question is so ambiguous...

    @a = split /\s+/, "my dog has fleas";
Re: How can one re-parse(?) a string to fill in variables
by kilinrax (Deacon) on Nov 23, 2000 at 23:07 UTC
    You're question is a little vague - but I'm guessing the problem is that you have code somewhat like the following (which i'll use as an example):
    #!/usr/bin/perl -w use strict; my $name = 'Alex'; my $greeting = 'Hello $name!'; print $greeting;
    Which you want to print 'Hello Alex', but currently it's printing 'Hello $name!'.

    The following code will do this, though is perhaps a little hack-ish, and some of our more experienced monks could enlighten both of us as to a more elegant method:
    #!/usr/bin/perl -w use strict; my $name = 'Alex'; my $greeting = 'Hello $name!'; $greeting =~ s|\$([\w_]+)|eval "\${$1}"|e; print $greeting;
Re: How can one re-parse(?) a string to fill in variables
by repson (Chaplain) on Nov 24, 2000 at 03:57 UTC
    You could try this if that is what the question means.
Re: How can one re-parse(?) a string to fill in variables
by brcjacks (Initiate) on Oct 31, 2013 at 13:58 UTC
    These days, why not just use double-quotes to interpolate the string. #!/usr/bin/perl -w use strict; my $name = 'Alex'; my $greeting = "Hello $name"; print $greeting

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://43151]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-08-20 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (105 votes), past polls