http://qs321.pair.com?node_id=11124906


in reply to Re: Printing first element of an array in worksheet
in thread Printing first element of an array in worksheet

Its printing the single element only after trying 2nd option

  • Comment on Re^2: Printing first element of an array in worksheet

Replies are listed 'Best First'.
Re^3: Printing first element of an array in worksheet
by davido (Cardinal) on Dec 10, 2020 at 16:48 UTC

    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:

    ARRAY

    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.


    Dave

      I am receiving all the elements from $responsetextall$i$j{set}{Client}{redirect_uris}; which was ARRAY of ARRAY references

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

      The issue is its not writing all the elements in single cell of a worksheet and its writing in separate cells. like the following 'http://abcd01.cpu.comp.com:80/AutosysPortal/' 'http://abcd01.cpu.comp.com:80/Da/', 'http://abcd01.cpu.comp.com:80/Ge/', 'http://abcd01.cpu.comp.com:80/PO/', 'http://abcd01.cpu.comp.com:80/g Can we split through "|" and put it into single cell?

        You probably mean join, not split, it's the inverse. Yes, you can:
        my $string = join '|', @{ $responsetextall[$i][$j]{set}{Client}{redire +ct_uris} };
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]