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

Hi guys, since Iam new to Perl, Iam asking myself what's the perlish way to constrcut the following string:
#{#string#10.0.0.1#;#string#10.0.0.2#;#string#10.0.0.3#}# and so forth +.
from on array like this :
my @dnsSuffixList=('10.0.0.1', '10.0.0.2', ... );
I do it this way, but ,aybe there is some better solution :-)
my @dnsSuffixList = qw ( 10.0.0.1 10.0.02 10.0.03 10.0.04 ); my $str = join ('#;#string#', @dnsSuffixList); $str =~ s/$str/#{#string#$str#}#/; # print $str;
Thanks in advance, Stephan

Replies are listed 'Best First'.
Re: Array / String
by moritz (Cardinal) on Oct 12, 2011 at 10:22 UTC

    Use join, and stick the first and last delimiter to the ends manually.

    Something like:

    my $result = '#(#string#' . join('#;#string#', @array) . '#)#;

    Or a bit prettier:

    my $result = sprintf '#(#string#%s#)#',join('#;#string#', @array);
      Thanks moritz, in the mean time i figured out this:
      my @dnsSuffixList = qw ( 10.0.0.1 10.0.02 10.0.03 10.0.04 ); my $str = join ('#;#string#', @dnsSuffixList); $str =~ s/$str/#{#string#$str#}#/; # print $str;
      What do you think is faster? sprintf or sed?
Re: Array / String
by RichardK (Parson) on Oct 12, 2011 at 12:23 UTC

    How about this ? -- Personally I think it's clearer and makes your intent more obvious, by YMMV ;)

    my @dnsSuffixList = qw ( 10.0.0.1 10.0.02 10.0.03 10.0.04 ); my $str = '#{'; $str .= "#string#$_#;" for @dnsSuffixList; $str .= '}#';