Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Compilation error

by truthseeker66 (Novice)
on Oct 09, 2012 at 15:59 UTC ( #998032=perlquestion: print w/ replies, xml ) Need Help??
truthseeker66 has asked for the wisdom of the Perl Monks concerning the following question:

When I run this script I keep getting an compilation error. How can I trouble shoot? Thank you for help

#!/usr/bin/perl use strict; use warnings; @array = qw(one two three four five six seven eight nine ten); print "The original array contains - @array \n"; @sorted = sort {$a <=> $b} @array; print "The sorted array contains @sorted \n";

C:\JPARK\JPERL>test.pl Possible unintended interpolation of @array in string at C:\JPARK\JPERL\test.pl line 6. Possible unintended interpolation of @sorted in string at C:\JPARK\JPERL\test.pl line 9. Global symbol "@array" requires explicit package name at C:\JPARK\JPERL\test.pl line 5.. . . .

Comment on Compilation error
Download Code
Re: Compilation error
by Fletch (Chancellor) on Oct 09, 2012 at 16:10 UTC

    The first step would be to actually read the error messages which say quite explicitly what's wrong. If those aren't clear enough, then pipe the errors through splain or use diagnostics.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Compilation error
by moritz (Cardinal) on Oct 09, 2012 at 16:11 UTC
    Possible unintended interpolation of @array in string

    That's only a warning.

    Global symbol "@array" requires explicit package name

    That's an error. You need to declare @array with my before the first use:

    my @array = qw(one two three four five six seven eight nine ten);
      1. When do I use 'my' before an array and when don't I?
      2. Now with this script I get an error. Does this code '{$a <=> $b}' work with only numbers?
      #!/usr/bin/perl use strict; use warnings; my@array = qw(one two three four five six seven eight nine ten); print "The original array contains - @array \n"; my@sorted = sort {$a <=> $b} @array; print "The sorted array contains @sorted \n";

      The original array contains - one two three four five six seven eight nine ten
      Argument "one" isn't numeric in sort at C:\JPARK\JPERL\test.pl line 7.
      Argument "two" isn't numeric in sort at C:\JPARK\JPERL\test.pl line 7.
      Argument "three" isn't numeric in sort at C:\JPARK\JPERL\test.pl line 7.
      . . . The sorted array contains one two three four five six seven eight nine ten

        1. When do I use 'my' before an array and when don't I?
        Only when you first declare the array variable.
        Does this code '{$a <=> $b}' work with only numbers?
        Yes. You want cmp (perlop and sort)
        use warnings; use strict; my@array = qw(one two three four five six seven eight nine ten); print "The original array contains - @array \n"; my@sorted = sort {$a cmp $b} @array; print "The sorted array contains @sorted \n";

        You should also use "code" tags for your error/warning messages.

        G'day truthseeker66,

        ++toolic's answers are spot on. Here's some additional information.

        "When do I use 'my' before an array and when don't I?"

        While you're focussing on arrays with this specific question, the information provided applies equally to declaring other types of variables, e.g. my $scalar, my @array and my %hash.

        So, as already stated, you declare your variable once then subsequently use it as many times as you want (without further declaration). If you declare a variable more than once you'll usually get a warning like this:

        $ perl -Mstrict -Mwarnings -e ' my $x; my $x; ' "my" variable $x masks earlier declaration in same scope at -e line 3.

        [Advanced usage: there are ways to declare variables with the same name more than once within the same piece of code - that's rarely, if ever, needed and best avoided as it's confusing and will often lead to unexpected errors.]

        my is the most frequently used way to declare variables - more in-depth information can be found in perlsub (particularly under Private Variables via my()).

        perlsub also has a lot of information about other ways to declare variables, including local, our, constant and state.

        "Does this code '{$a <=> $b}' work with only numbers?"

        You can reverse the order of the sort by swapping $a and $b:

        $ perl -Mstrict -Mwarnings -E ' my @numbers = (2, 1, 3); my @strings = qw{B A C}; say sort { $a <=> $b } @numbers; say sort { $b <=> $a } @numbers; say sort { $a cmp $b } @strings; say sort { $b cmp $a } @strings; ' 123 321 ABC CBA

        -- Ken

        If you want the sort order to appear in "numeric order of the names", you can define a special sort order. Here I use a hash table to give the "rules" to sort.

        #!/usr/bin/perl use strict; use warnings; my @array = qw(one two three four five six seven eight nine ten); print "The original array contains -\n @array \n"; my @sorted = sort @array; print "The sorted array contains:\n @sorted \n"; my %order =( one => 1, two => 2, three => 3, four => 4, five => 5, six => 6, seven => 7, eight => 8, nine => 9, ten => 10); @sorted = sort{ $order{$a} <=> $order{$b}} @sorted; print "Now The sorted array contains:\n @sorted \n"; __END__ The original array contains - one two three four five six seven eight nine ten The sorted array contains: eight five four nine one seven six ten three two Now The sorted array contains: one two three four five six seven eight nine ten
Re: Compilation error
by marquezc329 (Scribe) on Oct 09, 2012 at 18:18 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998032]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-12-27 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls