The case #1 says start with "el", match that, then allow any number of random characters while still allowing the "om" to match. It did that.

Case #2 is similar, except that the letters in-between "el" and last "om" are captured and printed.

Case #3: Your "pop" statement is nonsensical. I have no idea what you mean by this. Perhaps you meant to do that with "@matches"? I will demo that below.

Case #4 splits as "sentence" into 2 words.

#!/usr/bin/perl -w use strict; #case 1 my @matches = "hello awesome" =~ /(el).*(om)/; print "@matches\n"; #prints el om #case 2 @matches = "hello awesome" =~ /(el)(.*)(om)/; print "@matches\n"; #prints el lo awes om # (el)(lo awes)(om) #case 3 nonense!!! #print pop @awesome; #nonsensical ! #there is no array "awesome" and #therefore there is nothing to "pop" from. #case 4 @matches = split (/\s/,"hello awesome"); print "@matches\n"; #prints" hello awesome"
Update:I printed this in perhaps a more understandable way:
#!/usr/bin/perl -w use strict; #case A my @matches = "hello awesome home" =~ /(el).*(om)/; foreach (@matches) { print $_,"\n"; } ## el ## om #case B @matches = "hello awesome home" =~ /(el)(.*)(om)/; foreach (@matches) { print $_,"\n"; } ## el this is the (el) ## lo awesome h now there is another "om" before last om ## om this is last (om) #Case C #lets say that we only wanted this stuff between the first (el) #and the last (om)? And we just want a scalar. #this is done with an array slice. my $stuff = ("hello awesome home" =~ /(el)(.*)(om)/)[1]; print "$stuff\n"; #prints "lo awesome h" #Case D my @stuff = "hello awesome home" =~ /(el)(.*)(om)/; print pop (@stuff)."\n"; #prints: om print pop (@stuff)."\n"; #prints: lo awesome h print pop (@stuff)."\n"; #prints: el #Case E @stuff = "hello awesome home" =~ /(el)(.*)(om)/; print shift (@stuff)."\n"; #prints: el print shift (@stuff)."\n"; #prints: lo awesome h print shift( @stuff)."\n"; #prints: om

In reply to Re: accessing the result of a match as an array by Marshall
in thread accessing the result of a match as an array by intuited

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.