in reply to accessing the result of a match as an array

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

Replies are listed 'Best First'.
Re^2: accessing the result of a match as an array
by intuited (Novice) on Dec 26, 2009 at 04:37 UTC

    I guess the question was kind of vague.. Really I was just looking for a way to avoid a variable assignment and improve my golf scores, and maybe understand why perl didn't like what I was doing.

    print ( ( "hello awesome" =~ /(el).*(om)/ ) [1] ); is pretty much it. And yeah it was a trumped-up example that I didn't even bother to make realistic, to the point of not even looking it enough to notice that I totally spaced on the array name in the second statement.