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

Hi all,
Call me an idiot but I can't get this to work. I've been going round in circles. I'm sure it's obvious what I'm trying to do but I'll add a few notes.

I have an array(34 elements total) of hashes. I intend to keep a "print format" for the data type I'm accessing. The code below is just to test the mechanism.

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

Maybe someone could point me in the right direction?
TIA for all the help.

Replies are listed 'Best First'.
Re: dynamic string format?
by jlf (Scribe) on Jan 09, 2002 at 04:58 UTC
    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
      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??

Re: dynamic string format?
by metadoktor (Hermit) on Jan 09, 2002 at 06:39 UTC
    I don't know why your code is so complicated but then I don't know what you're going to use it for; however, if you don't need all that complexity you could use this instead:
    #!/usr/local/bin/perl -w
    
    use strict;
    
    my $format="%s%d%s%d";
    my @args=("stuff",0,"-",0);
    
    $args[1]=2;
    $args[3]=10;
    
    print "NEWNAME -> ",sprintf $format,@args;
    

    Simple.

    metadoktor

    "The doktor is in."

Re:(fix) dynamic string format?
by Anonymous Monk on Jan 09, 2002 at 06:23 UTC
    I changed the code as follows and it seems to be fine for my purposes.
    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";
    Becomes:
    my $dash = '-'; my ( $prefix, $suffix, @range, ); my $format = "%s%d%s%d"; my $args = '$prefix, $range[0], $dash, $range[1],'; #changed $prefix = "stuff"; $range[0] = 2; $range[1] = 10; my $new_name = sprintf "$format", eval "$args"; #changed print "NEWNAME -> $new_name\n";

    Thanks to all that looked