Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Printing first element of an array in worksheet

by chandantul (Beadle)
on Dec 09, 2020 at 17:57 UTC ( #11124894=perlquestion: print w/replies, xml ) Need Help??

chandantul has asked for the wisdom of the Perl Monks concerning the following question:

My code snippet is printing only first element of the array. This is printing the following This is printing only instead of all This is printing only '', '', '', '', ' Can you please let me know where its wrong here?

@redi = $responsetextall[$i][$j]{set}{Client}{redirect_uris}; for my $i (0..$#redi) { $worksheet->write($r, 6, [ $redi[$i]]); }

Replies are listed 'Best First'.
Re: Printing first element of an array in worksheet
by choroba (Archbishop) on Dec 09, 2020 at 18:03 UTC
    @redi = $responsetextall[$i][$j]{set}{Client}{redirect_uris};
    The right hand side of the assignment is a value from a deeply nested hash. The value of a hash is always a scalar, i.e. a single value.

    Show us the code where you populate the inner hash. You might need to use an anonymous array as the value:

    $responsetextall[$i][$j]{set}{Client}{redirect_uris} = [ '', '', '', '', '' ];

    and then you need to dereference the value:

    @redi = @{ $responsetextall[$i][$j]{set}{Client}{redirect_uris} };
    or (requires 5.20)
    @redi = $responsetextall[$i][$j]{set}{Client}{redirect_uris}->@*;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Printing first element of an array in worksheet
by davido (Cardinal) on Dec 09, 2020 at 18:06 UTC

    Look at this line:

    @redi = $responsetextall[$i][$j]{set}{Client}{redirect_uris}

    What does $responsetextall[$i][$j]{set}{Client}{redirect_uris} contain? Does it contain an array reference? You are assigning that array reference to @redi so that @redi contains:

    @redi = ( [....], );

    It contains a single element. That element is a reference to an array. You should have done one of two things. Either this:

    @redi = @{$responsetextall[$i][$j]{set}{Client}{redirect_uris}}

    ...or this:

    $redi = $responsetextall[$i][$j]{set}{Client}{redirect_uris} # and then... for my $i (0..$#$redi) { $worksheet->write($r, 6, [$redi->[$i]]); }

    The first one is an easier change, the second one is a little more memory efficient.

    See perlreftut, and perllol, perhaps.


      Its printing the single element only after trying 2nd option

        Your due diligence includes validating assumptions (sometimes just peppering code with print and print Dumper statements), and taking the time to understand the solutions presented.

        I mentioned in the original post that I assumed that $responsetextall[$i][$j]{set}{Client}{redirect_uris} contained an array reference. You could confirm that like this:

        print ref($responsetextall[$i][$j]{set}{Client}{redirect_uris}), "\n";

        Which should print:


        If it doesn't, your question was misleading. That's fine. If you use Data::Dumper you should be able to get better insight into what your data structure actually looks like:

        use Data::Dumper; print Dumper $responsetextall[$i][$j]{set}{Client}{redirect_uris};

        If the output to that is anything besides an array ref, you'll have to adjust how you're unpacking it later on.

        I composed the following test to verify that my solution works as intended (after removing the in, which must have sneaked in from some recent Python work):

        #!/usr/bin/env perl use strict; use warnings; my @responsetextall; $responsetextall[0][0]{set}{Client}{redirect_uris} = [qw(foo bar baz)] +; print ref($responsetextall[0][0]{set}{Client}{redirect_uris}), "\n"; my $redi; $redi = $responsetextall[0][0]{set}{Client}{redirect_uris}; for my $i (0 .. $#$redi) { print $redi->[$i], "\n"; }

        The output is:

        ARRAY foo bar baz

        So it is working, and is printing more than a single element. Also, your for loop is not the Perl way of doing things. If you don't need to care about the index, just iterate over the list:

        for my $element (@$redi) { print "$element\n"; }

        Nicer, right? Please look at perlref, perlreftut, and the REFERENCES section of perlcheat for additional information on how to manipulate references. perlsyn and perlintro should have more discussion on for or foreach loops.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11124894]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2021-04-17 18:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found