in reply to accessing the result of a match as an array
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.
Update:I printed this in perhaps a more understandable way:#!/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"
#!/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 |