1

I have a defined days array with the square bracket in Perl. I want to access each element of the array. A similar example from the code below(This is just a snippet of code):

@days = [a,2,3];
foreach(@days){print "$_\n";}
print "\n\n @days";

And output is:

ARRAY(0x2032950)


ARRAY(0x2032950)

I need to access the array elementS but I cannot change the @days declaration. The following code is not working as well:

 @days = [a,2,3];
    use feature qw<say>;    
    foreach(@days){print "$_\n";}
    print "\n\n @days\n";
    print "@$days\n";
    say $_ for $days->@*;

enter image description here

3
  • 2
    maybe you meant to initialize the @days array using a list instead of using an array reference? As it is now, you have @days with a single element which is a reference to an array. Commented Mar 18, 2020 at 21:30
  • 1
    Always add use strict; use warnings; to your scripts. Commented Mar 18, 2020 at 22:23
  • Yes, I did. I only added a similar example to part of the code. Commented Mar 18, 2020 at 22:49

3 Answers 3

5

Attn: OP - array declaration is not correct.

If you can not change array declaration (it is not clear what is the cause) then print them with following code

use strict;
use warnings;
use feature 'say';

my @days = ['a',2,3];

say for @{$days[0]};

say "Number of elements: " . scalar @{$days[0]};

Proper code should be

use strict;
use warnings;
use feature 'say';

my @days = ('a',2,3);

say for @days;

say "Number of elements: " . scalar @days;

Following piece of code demonstrates how array created, using this information is easy to figure out how to access stored values of array elements

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my @days = ['a',2,3];

say Dumper(\@days);

Output

$VAR1 = [
          [
            'a',
            2,
            3
          ]
        ];
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, it works :)Could you please provide me more details. Especially the role of ''say"
@user11086556 -- my @days = ['a',2,3]; creates an array with only one element which is a reference to an array ('a',2,3). It is not clear why the array was declared in such 'none conventional' way.
@user11086556 -- see extended part of the answer which demonstrates the content of the array.
@user11086556 - say similar with print $var . "\n", documentation say
2

I think you accidentally have an extra layer in your data.

When you use the square braces, you are actually using the anonymous array constructor. That returns an array reference, which is a scalar (single item). You probably didn't mean to assign that to an array since you get an array of one element where that one element is the reference. This effectively makes a double-level hash:

my @days = [ 'a', 1, 2 ];  # probably wrong

Since the array reference is a scalar, you likely mean to assign it to a scalar with the $ (single item) sigil. you don't use @ because the reference points to an array. The sigil is more about the container than the data:

my $days = [ 'a', 1, 2 ];

When you have the array reference, there are various ways to get its elements. Since it's a simple scalar (not a single element access to an array or hash), you can prefix it with @ (the multiple element sigil) to treat it as an array:

my @elements = @$days;

# OR
foreach my $element ( @$days ) {
    say "Element: $element";
    }

You can even interpolate that just like a named array:

say "Elements are @$days";

Comments

0

Here's a way to print the array reference:

#! /usr/bin/env perl

use warnings;
use strict;
use feature qw<say>;

my $arr_ref = [1,2,3];

say for $arr_ref->@*;

3 Comments

@G4113:- Yes, your code works fine. But once I change the variable it stops printing. use feature qw<say>; foreach(@days){print "$_\n";} print "\n\n @days\n"; print "@$days\n"; say $_ for $days->@*; Am I missing something. I appreciate your help.
You'll have to give me more details on the problem.
I added the code in question description again. The printout for the last added code is ARRAY(0x25b8950). it's still not printing the elements

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.