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

 



perlFun
Novice

Jun 14, 2013, 9:57 AM

Post #1 of 6 (284 views)
question about using m (match operator) with arrays Can't Post

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)


Laurent_R
Enthusiast / Moderator

Jun 14, 2013, 11:25 AM

Post #2 of 6 (275 views)
Re: [perlFun] question about using m (match operator) with arrays [In reply to] Can't Post

The first line of the code you posted simply takes the three first elements of the array (or possibly arrays: if the first array has only 2 elements, then the line picks the two elements of the first array and the first element of the second one) passed to the subroutine. $s1, $s2 and $msg are not arrays, but scalars.

But you should post examples of how this subroutine is called if you want more information.


perlFun
Novice

Jun 14, 2013, 11:37 AM

Post #3 of 6 (274 views)
Re: [Laurent_R] question about using m (match operator) with arrays [In reply to] Can't Post

I'm sorry, I think I posted the question in a weird way. I do realize that first line is simply taking in the arguments. Here let me give an example:

here's the sub:

sub _str_comp {
my ($s1, $s2, $msg) = @_;
if ( $s1 !~ m/$s2/ ) {
#do error logging stuff here
return 1;
}
return 0;
}#end sub _str_comp


here's an example of where it's being called:
(note: $a and $b are both references to hashes)
my @arrt = keys %{$a};
my @arr1 = grep(/\w+\.\w+\.\w+/,@arrt);
@arrt = keys %{$b};
my @arr2 = grep(/\w+\.\w+\.\w+/,@arrt);
if ( _str_comp( $#arr1, $#arr2, "a message") ) {
#stuff being done
}


my understanding:
I have two hash references, $a and $b. I get an array which holds the keys of the first hash. I grep this array to find matches of the form w+.w+.w+ (alphanumeric chars with two . delimeters). I similarly get an array of the keys of the has referenced by $b and grep it to find matches of that type. I send these grep results (which are arrays) to that _str_comp subroutine. My confusion is kind of what is being checked for in this context. will I get a positive result in that case so long as the arrays simply contain all the same values, regardless of indeces? (so for example say the keys for the hash referenced by $a are 54.jk.1 and 99.1.99, both would get grepped. Now say the keys for the hash referenced by $b are key1, 99.1.99, and 54.jk.1. then 99.1.99 and 54.jk.1 would get grepped. you'd end up sending the arrays @arr1 = (54.jk.1, 99.1.99) and @arr2 = (99.1.99, 54.jk.1) (references to such arrays). would those be equal under the "m" operation? Or does it actually just look at the scalar context of those arrays (which would be the size of the arrays?)


(This post was edited by perlFun on Jun 14, 2013, 11:50 AM)


FishMonger
Veteran / Moderator

Jun 14, 2013, 11:55 AM

Post #4 of 6 (269 views)
Re: [perlFun] question about using m (match operator) with arrays [In reply to] Can't Post

The sub is being passed the last index number of each array, not their contents.

So, if @arr1 had 3 elements and @arr2 had 5 elements, then the sub would receive 2 and 4 as its first 2 args. Using a regex to compare the numbers does not make sense. It would be better to test using the == numerical comparison operator instead.


(This post was edited by FishMonger on Jun 14, 2013, 11:56 AM)


perlFun
Novice

Jun 14, 2013, 12:00 PM

Post #5 of 6 (264 views)
Re: [FishMonger] question about using m (match operator) with arrays [In reply to] Can't Post

thank you so much for clearing this up. I am supposed to go in and rewrite this really long file and there's a lot of stuff in here like that. I guess it seemed kind of counter intuitive to me. This sub is in here, but it's never called to compare strings, it's only ever called to compare array sizes like this!!!! I don't know why it's even in the file.


Laurent_R
Enthusiast / Moderator

Jun 15, 2013, 12:45 PM

Post #6 of 6 (241 views)
Re: [perlFun] question about using m (match operator) with arrays [In reply to] Can't Post

Clling a sub to compare two numbers (max array subscripts) seems overkill to me. I would compare the array sizes and call the error processing subroutine only if array size differ. It seems clearer to me.

But, alright, the program might still be correct.

 
 


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

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