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

Hello Kind and Wise Monks:

I have tried many ways to de-reference a variable. And yes I looked at http://perldoc.perl.org/perlreftut.html. Specifically where they talk about ->. I have one bit of code that works, but I know it could be simpler.

. Here is the code that works:
for my $e (@$bbData) { print Dumper $e; my $d = $e->[0]; ##print Dumper $d; print "Col Zero: " . $e->[0] . " Col One: " . $e->[1] . " Col F +our: " . $e->[4] . " \n" ; ##print "Col Zero: " . $d . " \n" ; #foreach $tmp (@aDim) { # print $tmp }
This code prints the variables as expected. I would like to eliminate the loop, because there is only one element in the outer most array.

This bit of code does not work.

print "Error: " . Win32::OLE->LastError . "\n"; print Dumper $bbData; $e = $bbdata->[0]; print Dumper $e; print $SQLUpdate . $SQLUpdateWhere . "\n";
It produces the following output:
Error: 0 $VAR1 = [ [ 'VANGUARD TELECOM SERVICE ETF', 'B031NG6', '92204A884', 'US92204A8844', 'MSCITC' ] ]; $VAR1 = undef; update security set underlyingbloomberg = where securitykey =
I do not what to use the FOR loop because there is never more than one interior array. There must be a syntax that allows me to directly access the array contained in $bbdate[0].

many thanks for your kind assistance.

kd

Replies are listed 'Best First'.
Re: de-referencing @$
by kyle (Abbot) on Sep 30, 2008 at 19:14 UTC

    I say unto you, verily, use strict and warnings! Had you followed this sage advice, you'd have known immediately that you had mistyped a variable name.

    print Dumper $bbData; $e = $bbdata->[0]; print Dumper $e;

    On the first line, you refer to $bbData (with a capital D). On the second line, you refer to $bbdata (with a lowercase D).

    As to your question, you can access the element you want with $bbData->[0][0] or ${$bbData}[0][0] or $bbData->[0]->[0] or maybe a few more I'm not thinking of. I recommend the first.

      yes of course it help to use the correct variable name.
      OK I will start to strict and warings.

      Works now

      Many thanks.

      kd
Re: de-referencing @$
by moritz (Cardinal) on Sep 30, 2008 at 19:08 UTC
    my @e = @{$bbData->[0]}; print "Col Zero: " . $e[0] . " Col One: " . $e[1] . " Col Four: " . +$e[4] . " \n" ;
Re: de-referencing @$
by harleypig (Monk) on Sep 30, 2008 at 19:32 UTC

    You can take the first commenters solution one step further:

    printf "Col Zero: %s Col One: %s Col Four: %s\n", @{ $bbdata }[0,1,4]

    with appropriate changes for the data format.

    Harley J Pig
Re: de-referencing @$
by Krambambuli (Curate) on Sep 30, 2008 at 20:02 UTC
    Maybe you want
    my @data_elems = @{$bbData->[0]};
    ?

    Krambambuli
    ---