-4

I try to call a Subroutine in Perl but I get this error "Malformed prototype for main". I have a Subroutine Compare and I have to pass to it two integer.

#!/usr/bin/perl
@ListA=(1,2,3);
@ListB=(2,3,4);
@ListResult;
#AND

sub Compare($p1,$p2){                   
    if($p1 > sizeof(ListA) or $p2 > sizeof(ListB))
        { 
                  return;}                          

    if(ListA($p1) = ListB($p2)){                    
        push (@ListResult, ListA($p1)); 
        Compare($p1+1,$p2+1);           
        return;                         
    }
    if(ListA($p1) > ListB($p2)){        
        Compare($p1,$p2+1);             
        return;                         
    }
    else {                              
        Compare($p1+1,$p2);             
        return;                         
         }
    return;                             
}

Compare(1,1);

Please help me and explain how to correct this programm.

Daniel Böhmer
  • 14,463
  • 5
  • 36
  • 46
hhl
  • 97
  • 1
  • 1
  • 9
  • 8
    Add `use warnings; use strict;` and then read about perl: `perldoc perlintro` – xxfelixxx Oct 23 '15 at 12:41
  • 4
    How much research did you do on Perl syntax? Most of this looks nothing like Perl. – Dave Cross Oct 23 '15 at 13:57
  • 2
    @xxfelixxx, Starting with 5.24, `use warnings;` won't mean `use warnings 'all';` anymore, so new Perl programmers should use `use warnings 'all';`. – ikegami Oct 23 '15 at 14:59
  • @ikegami is there a 5.23 perldoc on perldoc.perl.org somewhere? – simbabque Oct 23 '15 at 15:03
  • 1
    @simbabque, You'd have to check the [Perl repo](http://perl5.git.perl.org/perl.git) [click "tree", then find the "pod" directory], but I don't think it's been committed yet. [Discussion](http://markmail.org/thread/5ywtsxrzlxdlt4o4). The idea is to add pedantic warnings that are turned on by `use warnings;`, but newcomers would be the ones that need these warnings the most. – ikegami Oct 23 '15 at 15:11
  • 6
    You appear to have made a wild guess about what Perl syntax looks like. You should learn a little Perl before asking the people at Stack Overflow to fix your guesses – Borodin Oct 23 '15 at 15:14

2 Answers2

5

This is wrong:

sub Compare($p1,$p2){      

Perl doesn't do it that way. This will be the source of the error you reported. (Technically it's prototyping, which is a perl feature that you shouldn't be using because it doesn't do what you might think).

You probably want instead:

sub Compare{      
    my ( $p1, $p2 ) = @_; 
     #etc.

Also: Turn on use strict; and use warnings;. They're annoying at first, but they really do a lot of good when it comes to programming gotchas.

Like:

sizeof(ListA) 

Isn't valid, because ListA is a bareword. Or a string, maybe. But what it isn't is the size of your @ListA. This is simpler than you think though - a list in a scalar context returns it's length. So you can simply do $p1 > @ListA and it will work.

And:

 if(ListA($p1) = ListB($p2)){     

Is:

  • using barewords again (so not referencing @ListA).
  • using () when it should be using []. (Accessing an array index is $arrayname[0])
  • Using = which is an assigment, rather than == (or eq for strings) which is a comparison.
Sobrique
  • 52,974
  • 7
  • 60
  • 101
4

This code really doesn't look much like Perl at all.

#!/usr/bin/perl

# You should always have "use strict" and "use warnings"
# at the top of your Perl programs.

# When you have "use strict", then you need to declare your
# variables, most commonly using "my".
@ListA=(1,2,3);
@ListB=(2,3,4);
@ListResult;
#AND

# Perl subroutines aren't defined like this
sub Compare($p1,$p2){
    # You need to use the @ at the start of the array name
    # whenever you are referring to the whole array.
    # And Perl doesn't have a "sizeof" function.
    # Also, your indentation style is *horrible* here :-)
    if($p1 > sizeof(ListA) or $p2 > sizeof(ListB))
        { 
                  return;}                          

    # I think you're trying to look up individual array elements
    # here. That's $array[$index], not array(index).
    # And in Perl, '=' is always an assignment operator. Here,
    # You probably want '==' which is a comparison operator.
    if(ListA($p1) = ListB($p2)){                    
        push (@ListResult, ListA($p1)); 
        Compare($p1+1,$p2+1);           
        return;                         
    }
    if(ListA($p1) > ListB($p2)){        
        Compare($p1,$p2+1);             
        return;                         
    }
    else {                              
        Compare($p1+1,$p2);             
        return;                         
         }
    return;                             
}

Compare(1,1);

Your program should be something like this:

#!/usr/bin/perl

use strict;
use warnings;

# I've changed the names of these variables, because in Perl
# arrays and lists are different things. These are arrays.
my @ArrayA = (1,2,3);
my @ArrayB = (2,3,4);
my @ArrayResult;
#AND

sub Compare {
  my ($p1, $p2) = @_;

  return if $p1 > @ArrayA or $p2 > @ArrayB;         

  if ($ArrayA[$p1] == $ArrayB[$p2]) {
    push (@ArrayResult, $ArrayA[$p1]);
    Compare($p1+1, $p2+1);
    return;
  }

  if ($ArrayA[$p1] > $ArrayB[$p2]){
    Compare($p1, $p2+1);
    return;
  } else {                              
    Compare($p1+1, $p2);
    return;                         
  }
  return;                             
}

Compare(1,1);

That will, at least, compile. But I have no idea whether or not it works because I don't know what it is supposed to do.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • thanks alot.but the programm dosenot wort yet.i want to write a perl programm to do this:get 2 list of document . determines the list of documents that contain A AND B.the algoritm must be write but it has still error of using == and > – hhl Oct 23 '15 at 14:26
  • 2
    I'm sorry, I don't understand what you're asking. The comment box is a terrible place to try to explain requirements. Why don't yo edit your question to explain what you want to do (with example inputs and outputs). – Dave Cross Oct 23 '15 at 15:03
  • we have 2 list A and B of id-numbers and we want to get a list which determines the list of id-numbers that contain in both list A AND B. list A=1,2,3,4 list B=1,2,8,9 answer =1,2 it meens that id-number 1 and 2 has both A and B – hhl Oct 23 '15 at 16:02
  • I'll ask once more. Please edit your question and include this information. – Dave Cross Oct 23 '15 at 16:23
  • Oh. Wait. I think you want [the intersection of two arrays](http://perldoc.perl.org/perlfaq4.html#How-do-I-compute-the-difference-of-two-arrays%3f-How-do-I-compute-the-intersection-of-two-arrays%3f) – Dave Cross Oct 23 '15 at 16:24
  • Finally, I solved it perfectly. Thank you so much for your help. – hhl Oct 27 '15 at 22:07
  • If my answer was useful to you, please consider accepting it. – Dave Cross Oct 28 '15 at 08:59