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

I am trying to push a load of information onto an array, some of which looks like this:

push(@text,"CACHEFILE C:\\wwwtools\\analog\\OldLogs\\Cache\\All\\@ParsedTime[$year]$Months{@ParsedTime[$month]}-2* \n");

push(@text,"CACHEFILE C:\\wwwtools\\analog\\OldLogs\\Cache\\All\\@ParsedTime[$year]$Months{@ParsedTime[$month]-1}* \n");

What im trying to do is print out something that looks like YYMM. Its a bit messy,but $Months is a hash to convert the day (in words) to the day in numbers. So what i want to do is actually take @ParsedTime[$Month] - C, c being a constant number, and sent that to the Months array.

So if im on month 10 it will use month 8 and month 9 and push them onto the array. My problem is that the program is outputting it as a literal statement, something like 12-1 where 12(the current month) was passed correctly to $Months and converted, but the -1 is added onto the string. I tried passing the value like this also:

push(@text,"CACHEFILE C:\\wwwtools\\analog\\OldLogs\\Cache\\All\\@ParsedTime[$year]$Months{@ParsedTime[$month-1]}* \n");
But that actually wont output anything at all. I am all out of ideas, does anyone have any they wish to share with me?

Replies are listed 'Best First'.
Re: Using expressions in string literals
by iburrell (Chaplain) on Dec 04, 2002 at 18:20 UTC
    Since your expressions don't need to be evaluated, you don't need to use eval() like other posters suggested. Try putting the expressions outside the string literal and use concatenation to compose the string:
    push(@text, "CACHEFILE c:\\wwwtools\\analog\\OldLogs\\Cache\\All\\" . +@ParsedTime[$year] . $Months{@ParsedTime[$month]-1} . "* \n");
    What are you trying to do with @ParsedTime[$year]. That is an array slice, not an array access. It should be $ParsedTime[$year].
Re: Using expressions in string literals
by broquaint (Abbot) on Dec 04, 2002 at 17:26 UTC
    There's no direct way of inserting expressions into strings, but you can do a sneaky scalar dereference e.g
    print "1 * 2 * 3 = ${\scalar 1 * 2 * 3}\n"; __output__ 1 * 2 * 3 = 6
    So what's happening there is that you're dereferencing a reference, and since you can put any old statement1 in the dereference you can use your expression (make sure there is a reference operator so there's something to dereference).

    Also, watch out on your array usage as you probably want $ParsedTime[$year], where as @ParsedTime[$year] is a single element array slice. This will be picked up by the ever useful warnings module.
    HTH

    _________
    broquaint

    1 this isn't strictly true, you can't have a straight compound statements in your dereference

Re: Using expressions in string literals
by pg (Canon) on Dec 04, 2002 at 17:08 UTC
    You have to use concatenation operator '.', no easy way.
Re: Using expressions in string literals
by Fletch (Bishop) on Dec 04, 2002 at 17:58 UTC
    $foo = "bar: @{[ $zoikes[2] + 2 ]}";
Re: Using expressions in string literals
by thezip (Vicar) on Dec 05, 2002 at 08:12 UTC
    You might also use sprintf, as in: push(@text, sprintf("CACHEFILE C:\\wwwtools\\analog\\OldLogs\\Cache\\All\\%02d%02d* \n", $ParsedTime$year, $Months{$ParsedTime$month-1}) );
Re: Using expressions in string literals
by slife (Scribe) on Dec 06, 2002 at 11:49 UTC
    join is useful for this kind of thing:
    push @text, (join '' => "CACHEFILE C:\\wwwtools\\analog\\OldLogs\\Cache\\All\\", $ParsedTime[$year], $Months{$ParsedTime[$month-1]}, '*', "\n", ), ;