in reply to Re^2: How to combine these 2 subroutines in one?
in thread How to combine these 2 subroutines in one?

Two obvious problems (there may be others):

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: How to combine these 2 subroutines in one?
by Anonymous Monk on Apr 03, 2014 at 14:19 UTC
    Aha, I see, will look further in my code, thank you for the tips :)
      Ok, I tried this
      sub clean_list { my $type_of_arg=ref(@{ $_[0] }); print "Type of argument passed is: $type_of_arg\n"; } open(IN, '<', 'ex5.acc') or die "Could not read file\n"; my @initial_array = <IN>; close IN; clean_list(@initial_array); clean_list(\@initial_array);

      and, if I pass the list as a reference it works OK, whereas, if I pass it as a simple array I get:
      Can't use string ("L13923") as an ARRAY ref while "strict refs" in use + at Lesson10.pl line 32.
      The L13923 is the first id that I have in my input file...

        The syntax @{ $_[0] } says: read the first argument passed into the sub clean_list, and then, treating it as a reference, dereference it to get an array. If this argument isn’t an array reference (as it probably won’t be if you passed in a list when calling the sub), then of course you’ll get an error message to that effect!

        Just test the first element, that is, call ref( $_[0] ) — and don’t try to dereference it until you’re sure it’s a reference, which is the whole point of testing it by calling ref.

        (I think maybe you should study perlreftut, then perllol, perldsc, and perlref, until you’re comfortable with references in Perl.)

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,