CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: question about using m (match operator) with arrays: Edit Log



perlFun
Novice

Jun 14, 2013, 9:57 AM


Views: 567
question about using m (match operator) with arrays

I am trying to figure out someone else's code and I come to this line:

my ($s1, $s2, $msg) = @_;
if ( $s1 !~ m/$s2/ ) {
.... (stuff happens) }

(note, this is in a subroutine, so $s1, $s2, and $msg are the arguments being passed in to this sub.) Now, when I first looked at this I assumed, what's going on is, someone is sending in two strings to be compared, and then a warning is printed out if they do/don't match.

But when I looked through the rest of the code, it seems every time this sub is getting called, arrays are being passed in to it. I was curious how the m (match operator) will function with arrays? does the order of elements inside matter? For example, say I have arrays @arr1 and @arr2, and say I populated them by using a grep function. Now let's say they have the same elements, but they aren't in the same indices. Would that m work when comparing them? (my understanding is they'd get passed in this subroutine as scalar references to the arrays. wouldn't that m end up then comparing their memory address location? will it "know" to look at the contents of the array?)

Is there a better way to do this comparison instead of using the "m"? (considering we might be wanting to compare arrays, scalars, etc.)

thanks so much!


(This post was edited by perlFun on Jun 14, 2013, 9:59 AM)


Edit Log:
Post edited by perlFun (Novice) on Jun 14, 2013, 9:58 AM
Post edited by perlFun (Novice) on Jun 14, 2013, 9:59 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives