Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

ask about array and hash

by agustina_s (Sexton)
on Jan 17, 2002 at 07:27 UTC ( #139433=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perlmonks.. I want to ask something which must be very easy for most of you but really weird for me as I am new in Perl. When I want to print something which is a reference to an array.. I try to use this :
print ${$entry->FTs->elements};
but it doesn't work. If I print without the ${ }, it will just give me output like :
The same thing also happen in hash.. it's just that the output is HASH(...). How should I print the content? Thanks in advanced

Replies are listed 'Best First'.
Re: ask about array and hash
by Malus (Acolyte) on Jan 17, 2002 at 08:34 UTC
    When you say:

    print ${$entry->FTs->elements};

    I think you meant to dereference it as an @rray and not as a $calar

    For example:

    getting the contents of a SCALAR reference: ${ $scalar_ref }

    getting the contents of an ARRAY reference: @{ $array_ref }

    If I understood the question, you meant this:
    print @{$entry->FTs->elements};

Re: ask about array and hash
by TStanley (Canon) on Jan 17, 2002 at 07:56 UTC
    Tested on Windows 98, ActiveState Perl 5.6, Build 630:
    #!perl -w use strict; my @array = qw(a b c d e f g); my $ref = \@array; print @$ref;
    The above prints out "abcdefg". What you are doing is effectively de-referencing the variable. You might also want to read up on perlref

    UPDATE:Corrected the shebang line. Thanks wog for pointing that out.

    "Suppose you were an idiot... And suppose you were a
    member of Congress... But I repeat myself." -- Mark Twain

Re: ask about array and hash
by Anonymous Monk on Jan 17, 2002 at 08:23 UTC
    References have to be dereferenced to print or iterate
    through. Your code was attempting to access an element
    of the list (array) instead of the entire array.

    To access a single element of a referenced array you can:
    print ${$entry->FTs->elements}[$number]
    Make sure you use 'use strict' it would have thrown an
    error when the example you gave was used, I am not
    saying it would be any clearer as to how to solve it
    but at least you would have a starting point :^)

    For an array reference you would iterate as:
    print "$_\n" for @{$entry->FTs->elements};
    for a hash ref:
    print "$_ = $hashref->{$_}\n" for keys %{$hashref};

    See 'perldoc perlref' for more information.
    or on the web at: perlref
Re: ask about array and hash
by busunsl (Vicar) on Jan 17, 2002 at 11:39 UTC
Re: ask about array and hash
by reclaw (Curate) on Jan 17, 2002 at 10:03 UTC
    perlreftut - Mark's very short tutorial about references. This got me on the right track figuring it out.
      perldsc is pretty useful too.


Re: ask about array and hash
by xtype (Deacon) on Jan 17, 2002 at 09:04 UTC
    Or you can loop through them to print the elements:
    #!/usr/bin/perl -w use strict; my @array = qw(a b c d e f g); my %hash = ( '1st' , 'a', '2nd' , 'b', '3rd' , 'c', ); while (<@array>) { print; } foreach my $key (sort keys %hash) { print "$key: $hash{$key}\n"; }
        Indeed. I wanted to show that there was more than one way to print and or access the data in an array or hash. Hence my using a foreach loop in the second example, also showing use of sort(). I am sorry if my globbing offended you in anyway. -xtype

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-07-04 00:25 GMT
Find Nodes?
    Voting Booth?

    No recent polls found