Access to two-dimensional array in Perl

Trying to learn Perl. I have an array populated by cities. I want to pass an array by reference to a subroutine and print each city for output. However, I have the following problems:

1) I can access each element before my while loop in the routine. But I cannot access the elements inside the while loop. I get an error message:

...
Use of uninitialized value in print at <filename> line 44, <GEN2> line 997 (#1)
Use of uninitialized value in print at <filename> line 44, <GEN2> line 998 (#1)
...

Below is the code. I commented on what prints and what doesn't (I tried to cut code that is not needed for my explanation ...):

@cities;

#Assume cities is loaded successfully
&loadCities(getFileHandle('cities.txt'), $NUM_CITIES, \@cities);
&printElements(getFileHandle('names.txt'), \@cities);

sub printElements{

    my $counter = 0;
    my $arraySize = scalar $_[1];

    # Prints fine!!!
    print @{$_[1][($counter)%$arraySize];

    while ((my $line = $_[0]->getline()) && $counter < 1000){

        #Doesn't print. Generates the above error
        print @{$_[1][($counter)%$arraySize];

        $counter += 1;
    }
}

2) Perl syntax confuses me. I do not understand what is happening with @ {$ _ [1]} [0]. Trying to figure it out.

  • $ _ [1], treat the value in this place as a scalar value (memory array address)
  • @ {...}, interpret what the memory address is stored as an array
  • @{...} [x], x

?

+3
5

: use strict; use warnings; script. , , .

: print @{$_[1][($counter)%$arraySize]; }. $counter.

, / - my $arraySize = scalar @{$_[1]};.


. .

my @array = (1, 2, 3);

.

my $array_ref = \@array;

, @{...}. .

print @{$array_ref};

, .

my $array_ref = [1, 2, 3];
print @{$array_ref}; # prints 123

Perl 2- . :

my @array = ( ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'] );
print @{$array[1]}; # prints def

.

my @array = ( ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'] );

example(\@array); # pass in an array reference

sub example {
    my @arr = @{$_[0]}; # use the array reference to assign a new array
    print @{$arr[1]};

    print @{$_[0][1]}; # using the array reference works too!
}

2-d.

my @array = ( ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'] );
example(\@array);
sub example {
    my @arr = @{$_[0]};
    for my $ref (@arr) {
        print @{$ref};
    }
} # prints abcdefghi

, printElements.


. :

print @{$ref};

, .

print @{$ref} . "\n";

? ! ?

join.

print join(" ", @{$ref}) . "\n";

, , . , while: fooobar.com/questions/2112137/... : Perl

+3

, -> munge-it-all-together.

:

@{$_[1]}[0].

Try

$_[1]->[0];

. , . , $_[1] , .

, - @_. , , .

sub print_elements {
    my $file_handle      = shift;   # This isn't a "reference", but an actual file handle
    my $cities_array_ref = shift;   # This is a reference to your array

    my @cities = @{ $cities_array_ref };  # Dereferencing makes it easier to do your program

, , - , . , . @_, , . @_ , , , , .

, :

sub printElements {
    my file_handle        = shift;
     my $cities_array_ref  = shift;

    my @cities = @{ $cities_array_ref };
    my $counter;
    my $array_size = @cities;     # No need for scalar. This is automatic
    while  ( my $line = $file_handle->getline and $counter < 1000 ) {
        chomp $line;
        my $city_number = $counter % $array_size;
        print $cities[$city_number]. "\n";
        $counter += 1;
    }
}

, , , , . , . , . , $counter % $array_size . , , .

, $line, getline. - ?

, while:

sub printElements {
    my file_handle        = shift;
    my $cities            = shift;   # This is an array reference!

    my $counter;
    my $array_size = @{ $cities };   # I need to deref to get an array
    while  ( my $line = $file_handle->getline and $counter < 1000 ) {
        chomp $line;
        my $city_number = $counter % $array_size;
        print $cities->[$city_number]. "\n";   # That it!
        $counter += 1;
    }
}

, -> , $cities , ? , ${$cities}[$city_number].

+3

.

print @{$_[1][($counter)%$arraySize];

, :

print $_[1]->[($counter)%$arraySize];

arraySize.

- ,

print "@{$_[1]->[($counter)%$arraySize]}";
+1

, №1 ( # 2, - ).

my $arraySize = scalar $_[1];

my $arraySize = @{$_[1]};

, .

, $_ [1] , , $ .

0

! . :

sub printElements{

    my $counter = 0;
    my $fh = $_[0];
    my @array = @{$_[1]};
    my $arraySize = scalar @array;

    # Prints fine!!!
    print @array[($counter)%$arraySize];

    while ((my $line = $fh->getline()) && $counter < 1000){

        #Doesn't print. Generates the above error
        print @array[($counter)%$arraySize];

        $counter += 1;
    }
}

, - , , (, ), " ", . , , ...

0

All Articles