How to compare array items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sali444
    New Member
    • Apr 2010
    • 2

    #1

    How to compare array items

    Hi
    Help Me Please
    Code:
    #compare @a & @b elements.
    #if $a[x] = $b[y] then add to @neo
    #print sorted list of @neo elements
    
    @a = qw(BB Ninja an FFF);
    @b = qw(ali reza cd an);
    @neo = qw();
    my $sza = @a;
    my $szb = @b;
    
    for($j=0; $j <= $szb; $j++)
    {
        for($i=0;$i <= $sza;$i++)
        {
        $c1 = $a[$j];
        $c2 = $b[$i];
            if($c1==$c2) 
            {
            unshift(@neo,$c2);
            print $c1."  ==  ".$c2."\n";
            }
        }
    }
    print join("\n",sort @neo);
    Last edited by numberwhun; May 19 '10, 02:21 AM. Reason: Please use CODE TAGS!!!
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    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.

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      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)

      Comment

      • sali444
        New Member
        • Apr 2010
        • 2

        #4
        i want add joint elements to @neo.
        and print ...

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          Originally posted by sali444
          i want add joint elements to @neo.
          and print ...
          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.

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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

            Comment

            Working...