• For some reason, printing out $1 only prints 1 star when the regex $HAIR is clearly matching 3 stars
  • For some reason, my attempt to replace 3 stars at the start of a string with the empty string is clearning the whole string.

    Could someone please help me with this? Thanks!

    my @question; sub make_question { my ($question,@answer) = @_; [ $question, \@answer ]; } push @question, make_question 'What is an example of autovivification in Perl?', '***my %x; $x{y}->[2]->{jum} ;', 'sub y { my ($self,$x) = @_ }', 'my $end = ($end > rand) ? $end : rand;'; ; for (@question) { printf "Question %d: %s\n", ++$count, shift @$_; my $HAIR = '^([*]){3}'; my $count; for my $answer (@{$_->[0]}) { if ($mode eq 'key') { if ($answer =~ /$HAIR/) { $answer = $1; } else { $answer = ''; } } else { $answer = s/$HAIR//; } printf "\t%d. %s\n", ++$count, $answer; } print "\n"; }

    In reply to regex to substitute 3 stars at beginnning of string with none? by princepawn

    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.