By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

How to compare array items

P: 2
Hi
Help Me Please
Expand|Select|Wrap|Line Numbers
  1. #compare @a & @b elements.
  2. #if $a[x] = $b[y] then add to @neo
  3. #print sorted list of @neo elements
  4.  
  5. @a = qw(BB Ninja an FFF);
  6. @b = qw(ali reza cd an);
  7. @neo = qw();
  8. my $sza = @a;
  9. my $szb = @b;
  10.  
  11. for($j=0; $j <= $szb; $j++)
  12. {
  13.     for($i=0;$i <= $sza;$i++)
  14.     {
  15.     $c1 = $a[$j];
  16.     $c2 = $b[$i];
  17.         if($c1==$c2) 
  18.         {
  19.         unshift(@neo,$c2);
  20.         print $c1."  ==  ".$c2."\n";
  21.         }
  22.     }
  23. }
  24. print join("\n",sort @neo);
  25.  
Apr 8 '10 #1
Share this Question
Share on Google+
5 Replies


Expert
P: 70
Add "use warnings;" to your code, then eliminate all the warning messages that are generated.

Then add "use strict;' and declare all variables with "my".

Read: http://www.perlmonks.org/?node_id=111088

If your code does not behave as desired, post your actual output and your expected output.

Use CODE tags around your code when you ask questions.
Apr 8 '10 #2

Expert 100+
P: 785
You forgot to tell us where your problem is.

Or did you just copy your homework assignment, which you are not allowed here? (the comment in the first lines leads to that assumption)
Apr 9 '10 #3

P: 2
i want add joint elements to @neo.
and print ...
Apr 10 '10 #4

Expert 100+
P: 785
@sali444
You already did this in your code:
"unshift(@neo,$c2);" adds the element provided in $c2.
And "print join("\n",sort @neo);" already prints the whole sorted array.
Apr 11 '10 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
Sorry I didn't notice this thread sooner.

Please DO NOT post questions in the "Insights" forum. That is reserved solely for tutorials and articles. Please post all questions under the answers forum(s).

Regards,

Jeff
May 19 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.