Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Extract&Print row from two-dimensional array

by petecm99 (Pilgrim)
on Nov 09, 2010 at 20:07 UTC ( [id://870396]=perlquestion: print w/replies, xml ) Need Help??

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

Easy question (I hope) about joining/printing array elements...I'm probably overlooking something simple.

#@cell is a two-dimensional array [row][col] of Excel elements my @rowOneCells = $cell[0]; #get row one as new array my $rowOne = join (", ", @rowOneCells); #Show row one in Win32::GUI text box $TxtStatus->Append("Found header:\r\n$rowOne\r\n");


For some reason this prints gibberish such as ARRAY(0x2b59f14) instead of the joined array elements.

Brethren, bless you for healing my blindness.

Replies are listed 'Best First'.
Re: Extract&Print row from two-dimensional array
by zentara (Archbishop) on Nov 09, 2010 at 20:18 UTC
    It looks to me like you are getting an arrayref from $cell[0], not an array. So you need to dereference the ARRAY(0x2b59f14) into an array before joining.

    untested

    my $rowOneCells = $cell[0]; #get row one as new arrayref my $rowOne = join (", ", @{$rowOneCells} );

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Brilliant! Thanks for that, worked like a charm...
Re: Extract&Print row from two-dimensional array
by AnomalousMonk (Archbishop) on Nov 09, 2010 at 22:56 UTC

    Just to guild the lily of zentara's reply:

    >perl -wMstrict -le "my @ra_2d = ( [ qw(a b c) ], [ qw(x y z) ], [ qw(p q r) ], ); ;; print join '-', $ra_2d[1] ; print join '-', @{ $ra_2d[1] }; ;; for my $arrayref_row (@ra_2d) { print join ':', @$arrayref_row; } " ARRAY(0xafc9dc) x-y-z a:b:c x:y:z p:q:r

    See perlref, perlreftut, perllol, perldsc.

    And, as always, Data::Dumper and its ilk help to visualize just what you're dealing with.

Re: Extract&Print row from two-dimensional array
by NiJo (Friar) on Nov 09, 2010 at 20:43 UTC
    You did not tell us exactly where @cell comes from. I suppose it is an object created by one of the many CPAN modules to work with Excel files. By overloading of operators your object can look like a plain array. Please have a closer look at the module documentation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://870396]
Approved by lostjimmy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found