in reply to dynamic string format?

my $dash = '-'; my ( $prefix, $suffix, @range, ); my $format = "%s%d%s%d"; my $args = [$prefix, $range[0], $dash, $range[1],]; $prefix = "stuff"; $range[0] = 2; $range[1] = 10; my $new_name = sprintf ("$format", @$args); print "NEWNAME -> $new_name\n";
Should print: NEWNAME -> stuff2-10 But I keep getting: NEWNAME -> 0-0

The problem here is that you are initializing @args with a list containing the values of $prefix, $range[0], $dash, $range[1] before the values are set. In other words, you are assigning a list containing [undef, 0, '-', 0] to $format. If you move the @args assignment below the assignments for $prefix and $range[0..1] you should get the results you were expecting.

HTH,
Josh

Replies are listed 'Best First'.
Re: Re: dynamic string format?
by Anonymous Monk on Jan 09, 2002 at 05:38 UTC
    I thought about that but... the flow of the program kinda requires that I set up a "format" to follow... much earlier than the actual setting of the values.

    Any other ideas??