Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Dereferencing the array and printing by Concatenation?

by gube (Parson)
on Apr 24, 2006 at 12:23 UTC ( #545258=perlquestion: print w/ replies, xml ) Need Help??
gube has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

While, dereferencing and printing the reference array i am getting the exact values stored in the array. But, while dereferencing the array and printing by concatinate with some text i am getting length of the array do i need some changes in my code ?

#!/usr/local/bin/perl use strict; use warnings; my @a = qw/a b c/; my $a = \@a; print @{$a}; # Printing correct output abc... # It prints length of the array why? print "Dereference and Printing the Reference array " . @{$a};

2006-04-26 Retitled by planetscape, as per Monastery guidelines
Original title: 'Dereferencing the array and printing by Concatinate?'

Comment on Dereferencing the array and printing by Concatenation?
Download Code
Re: Dereferencing the array and printing by Concatenation?
by gaal (Parson) on Apr 24, 2006 at 12:33 UTC
    This has nothing to do with references.

    my @a = qw/a b c/; print "Here are the elements. Notice the comma ", @a; print "Here's the size. Notice the dot " . @a;

    The concatenation operator "." puts the expression @a in scalar context. This also would print the size:

    print "Notice the comma, but also 'scalar' ", scalar @a;

      Hi gaal,

      Thanks for your reply if i use comma it's working fine..This is nothing to do with reference <reply> I have used a long code, in that code while debug i have doubt so, i paste the small sample code...Thx..

Re: Dereferencing the array and printing by Concatenation?
by sen (Hermit) on Apr 24, 2006 at 12:35 UTC

    Hi gube try this

    print "Dereference and Printing the Reference array " , @{$a};
Re: Dereferencing the array and printing by Concatenation?
by davorg (Chancellor) on Apr 24, 2006 at 12:43 UTC

    Or...

    print "Dereference and Printing the Reference array @{$a}";

    That has the bonus of putting spaces (or whatever $" is set to) between the elements of your array.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Dereferencing the array and printing by Concatenation?
by TedPride (Priest) on Apr 24, 2006 at 12:45 UTC
    Actually, there's no need to use {} in this situation, since the reference is a simple variable, not something like @{$arr[0]}. You can just do @$ref. Also, as a matter of general policy, you don't want to use $a, since it's one of the variables Perl uses for sorting.
    use strict; use warnings; my @x = qw/a b c/; my $x = \@x; print "Array elements with spaces: @$x";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-09-02 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls