Question

Have the below data structure. I want to print the entire array for the key TUESDAY. Tried the below way but it's not working. I don't want to have an additional statement of taking the array reference to a variable and printing out later. I want to do it in single statement in the print function.

my $FILE_LIMIT = {
    CHECK => "ON",
    ISANE => {
        CHECK     => "ON",
        MONDAY    => 33,
        TUESDAY   => [10, 20, 30, 40],
        WEDNESDAY => 12,
        THURSDAY  => 13,
        SATURDAY  => 14,
        SUNDAY    => 15
    } };

print "array val: " . $FILE_LIMIT->{ISBANE}->{TUESDAY}[1 .. $#] . "\n";
Was it helpful?

Solution

print "array val: " . join (' ', @{ $FILE_LIMIT->{ISANE}->{TUESDAY} }), "\n";

OTHER TIPS

As you have guessed, the array reference is at $FILE_LIMIT->{ISANE}{TUESDAY}, so dereference it using

print "array val: @{$FILE_LIMIT->{ISANE}{TUESDAY}}\n";

output

array val: 10 20 30 40
print"  @{ $FILE_LIMIT->{ISANE}->{TUESDAY}}\n";

output:

  10 20 30 40  

you are basically trying to take out the array slice in your code, that is useful if you want to take out only specific elements of the array, try to understand the below code that would take out the some elements from the array.

my @test = @{ $FILE_LIMIT->{ISANE}->{TUESDAY}};
print " @{$FILE_LIMIT->{ISANE}->{TUESDAY}}[1 .. $#test] \n";

output:

20 30 40
my $FILE_LIMIT = {
    CHECK => "ON",
    ISANE => {
        CHECK     => "ON",
        MONDAY    => 33,
        TUESDAY   => [10, 20, 30, 40],
        WEDNESDAY => 12,
        THURSDAY  => 13,
        SATURDAY  => 14,
        SUNDAY    => 15
    } };

print $_,"\n",foreach(@{$FILE_LIMIT->{ISANE}->{TUESDAY}});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top