The following sample may help clear up the interpolation issue. Don't worry too much about the first few lines. The key stuff is inside the for loop. The regex pulls the two parts out of the URL that you want to munge together and the double quoted string does the munging by interpolating the contents of the variables into the string. Note the use of the {} to let the Perl interpreter know that the trailing _ is not part of the variable name.

use warnings; use strict; my $root = 'http://gd2.mlb.com/components/game/aaa/year_2007/month_08/ +day_06/gid_2007_08_06_quiaaa_yucaaa_1/batters/'; my @batters = map {"$root$_"} qw(112039.xml 120107.xml); for my $url (@batters) { my ($prefix, $file) = $url =~ m!/(gid\w*)/batters/(.*)!; print "${prefix}_$file\n"; }

Prints:

gid_2007_08_06_quiaaa_yucaaa_1_112039.xml gid_2007_08_06_quiaaa_yucaaa_1_120107.xml

The double quoted string following the print can drop into your open, but remove the \n at the end and remember to use the three parameter version and to check the result.


DWIM is Perl's answer to Gödel

In reply to Re^3: How to Save Fetched Web Files as "path/$string.xml" by GrandFather
in thread How to Save Fetched Web Files as "path/$string.xml" by nase

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.