Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

String interpolation

by hotshot (Prior)
on Dec 09, 2004 at 12:55 UTC ( #413508=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I have a string defined as follows:
my $str = 'params.$idx.arg';
$idx will be assigned with integer values in a loop as follows:
for my $idx (0..10) { &do_something($str); }
The thing is that I want $str in the loop to be interpulated with the value of $idx in each loop iteration (the above code will not take there of course), meaning do_something() will get a different parameter each loop iteration ('params.0.arg', 'params.1.arg', etc.).
How can I achive that?


Retitled by davido: Corrected spelling of 'interpolation' to improve searchability.

Replies are listed 'Best First'.
Re: String interpolation
by fglock (Vicar) on Dec 09, 2004 at 13:03 UTC
    use strict; my $str = sub { "params.$_[0].arg" }; sub do_something { print +shift, "\n" } for my $idx (0..10) { do_something( $str->($idx) ); }


    params.0.arg params.1.arg params.2.arg params.3.arg params.4.arg params.5.arg params.6.arg params.7.arg params.8.arg params.9.arg params.10.arg
Re: String interpolation
by gellyfish (Monsignor) on Dec 09, 2004 at 13:19 UTC

    An alternative to the other suggestions is to use (s)printf:

    my $str = 'params.%d.arg'; + for my $idx (0 .. 9 ) { printf $str, $idx; }
    You will need to be careful in sanitizing the contents of $str if all or part of it is being taken from user (or otherwise untrusted) input .


Re: String interpolation
by reneeb (Chaplain) on Dec 09, 2004 at 12:56 UTC
    use " instead of ':
    my $str = "params.$idx.arg";

    That means for your code:
    for(0..10){ my $str = "params.$idx.arg"; do_something($str); }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: String interpolation
by edan (Curate) on Dec 09, 2004 at 13:09 UTC

    I think the easiest solution is using the dreaded string eval:

    for my $idx (0..10) { my $interp_str; eval "\$interp_str = qq($str)"; do_something($interp_str); }

    All the usual caveats about untainting user inputs apply, of course.


Re: String interpolation
by kelan (Deacon) on Dec 09, 2004 at 13:24 UTC

    Do your own interpolation:

    for my $idx ( 0 .. 10 ) { my $newstr = $str; $newstr =~ s/\$idx/$idx/; do_something( $newstr ); }

Re: String interpolation
by kappa (Chaplain) on Dec 09, 2004 at 15:19 UTC
    That's eval thing.
    #! /usr/bin/perl -w use strict; my $str = '"number $idx\n"'; foreach my $idx (1 .. 10) { print eval($str); }
Re: String interpolation
by hotshot (Prior) on Dec 09, 2004 at 15:02 UTC
    Thanks guys, but fglock's solution is the best for me, I wanted to avoid using additional temporary parameters (I already knew these options), but thanks anyway.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://413508]
Approved by gellyfish
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2023-03-22 20:00 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.