in reply to Creating filenames

htmanning:

What you're doing wrong is assuming that all titles are going to have 12 words. Instead, try this:

@words = split / /, $titleclean; $filename = "$year-$mon-" . join("-",@words) . ".html";

The join bit in the middle tells perl to string @words together with a dash between each one.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Creating filenames
by JavaFan (Canon) on Apr 02, 2012 at 20:51 UTC
    I'd write the last statement as:
    $filename = join "-", $year, $mon, @words; $filename .= ".html";

      JavaFan:

      Yes, that's much cleaner than mine.

      ...roboticus

      When your only tool is join, all problems become a matter of selecting the proper delimiter.

Re^2: Creating filenames
by ww (Archbishop) on Apr 02, 2012 at 20:04 UTC
    roboticus solution is far better than my suggestion.

    But it does give me the excuse for a tangential observation: If your titles contain as many as 12 words (and some of them are l...o...o...n...g words) you're going to end up with a $filename that's terribly long -- if they average 4 chars per word and you're using a four digit year and a three letter month, that's a filename nearly 70 chars long (and heaven knows what a full path might look like).

Re^2: Creating filenames
by htmanning (Friar) on Apr 02, 2012 at 20:19 UTC
    Thanks. This works, but @ww makes a good point that I should count the number of words, stick it in a variable, then the loop doesn't have to go around 12 times. This seems to work:
    @words = split (/ /, $titleclean); $numwords+=@words; $filename = $year."-".$mon; for ($i=0; $i<$numwords; $i++) { $filename = "$year-$mon-" . join("-",@words) . ".html"; }
    Look okay?
      No.

      roboticus way goes around only as many times as there are words in the title.

        Thanks. You're right. roboticus's way works. I misunderstood.