#!/usr/bin/perl -w %hash = (); open(FILE, "story.txt") || die ; #opens file story.txt while ( $story[$i++] = <FILE> ){ } #moves story.txt to @story close (FILE); #closes Story.txt foreach $list ( @story ) { @temp = split(/ /, $list); #puts word by word in @temp $i=0; while ($i <= $#temp) { $word=$temp[$i]; if ( $word =~ /^\[/ && $word =~ /$\]/) { if (! exists $hash { $word } ) { $auxiliary_word = $word; $auxiliary_word =~ tr/\[/ /; #removes front bracket $auxiliary_word =~ tr/\].,?/ /; #removes rear bracket & pu +nctuation $capitol = 0; if ($word =~ /^\[[A-Z]/) #checks for capitol letter { $capitol = 1; } Replace($word,$capitol); #function call to enter own word +s } } $i++; } } Print(); #print final madlib story #***************************************************************** sub Replace #user enters own words { print" Please enter a $auxiliary_word: "; $new_word = <STDIN>; if ($capitol == 1 ) { $new_word =~ s/(^[a-z]+)/\u$1/; #makes first letter capitol } chomp ( $new_word ); $hash { $word } = $new_word; #adds capitol letter t +o hash } #********************************************************************* sub Print #function to print new story { foreach $list ( @story ) { @temp = split(/ /, $list); $i=0; while ($i <= $#temp) { $word = $temp[$i]; if ($word =~ /^\[/ && $word =~ /$\]/) #if ( $word =~ m/\B[[A-Za-z]]/g) { $temp[$i] = $hash { $word }; } $i++; } $final = join(' ', @temp); #joins story together print $final; #prints new story } }
Strangeness in Cars I once owned a [noun] that was [adjective] only when , in a [Make-of-car]. Some [plural-noun] thought, [adverb], that this was [another-adjective], but what did they [verb]. Anyway, now I am [comparative], and don't [verb] so much, even when in [make-of-car] cars. Do you think that's [another-adjective], or should I [strange-action-verb]?

In reply to Re: Re: pattern matching by fasteddie
in thread pattern matching by fasteddie

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.