Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting these loop errors in basic Perl

Tags:

perl

I have the following code in Perl. I am very new to the language:

#!/usr/bin/perl

use strict;
use warnings;

my $date = $ARGV[0];

my $symbols = ('A', 'B', 'C');


foreach $symbol (%symbols)
{
    my $print = "$symbol";
    print "$print";

}

Getting:

Useless use of a constant in void context at (line of %symbols)
and
Global symbol "$symbol requires explicit package name at ..."
and
Global symbol "%symbols" require explicit package. name at ..."
like image 903
SuperString Avatar asked Feb 13 '26 15:02

SuperString


2 Answers

You are using an Hash when an Array is all that is needed.

 #!/usr/bin/perl


    use strict;
    use warnings;

    my $date = $ARGV[0];

    my @symbols = ('A', 'B', 'C');


    foreach my $symbol (@symbols)
    {
        print $symbol;

    }
like image 88
Bruce Avatar answered Feb 15 '26 12:02

Bruce


1) Your $symbols should be @symbols, since it's an array. Later in the foreach, %symbols should be @symbols.

2) The $symbol is not declared. Say foreach my $symbol... instead.

like image 45
Amadan Avatar answered Feb 15 '26 13:02

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!