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

difference between @, $

by soubalaji (Sexton)
on Dec 07, 2011 at 15:39 UTC ( #942269=perlquestion: print w/ replies, xml ) Need Help??
soubalaji has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,
I have a doubt in perl. I am having two types
@a = ['1','2','3','4'];
$a = ['1','2','3','4'];
I want to know the difference between these two. Any body can explain this, what the datatype these
Thanks and regards,
Jind

Comment on difference between @, $
Re: difference between @, $
by choroba (Abbot) on Dec 07, 2011 at 15:44 UTC
    In the first case, you are assigning to an array @a. The first element of the array will be the reference to the anonymous array containing 4 values.
    In the second case, you assign the reference directly to a scalar variable $a.
    Or, as SuicideJunkie put it in the CB: in the first case, $a[0][2] == 3 and in the second case, $a->[2] == 3
Re: difference between @, $
by sumeetgrover (Scribe) on Dec 07, 2011 at 16:02 UTC

    I like choroba's answer, it's exactly the way it works. On a theoretical note:
    * List/Array assignment: @list=('el1','el2','el3');
    (Note, the parenthesis indicate that a regular array initialisation is being carried out.)
    * Array Reference Assignment: $aref=['el1','el2','el3']
    (Note, the scalar variable $aref is being assigned a reference to a list, enclosed in square brackets).

    Enjoy!

      Note, the parenthesis indicate that a regular array initialisation is being carried out.
      Eh, no. The list assignment actually stems from the LHS of the assignment being an array, not from the RHS having parens. The parens are needed because the comma has a lower precedence than the assignment operator.
      $a = (1, 2); # Parens, but scalar assignment @a = 1; # No parens, but list assignment
Re: difference between @, $
by si_lence (Deacon) on Dec 07, 2011 at 17:01 UTC
    Just to add to the answers already given:
    I find it really helpful to visualize data structures with Data::Dumper. It helps me to see the difference. So
    use Data::Dumper; my @a = ['1','2','3','4']; my $a = ['1','2','3','4']; print Dumper(\@a); print Dumper($a);
    gives
    $VAR1 = [ [ '1', '2', '3', '4' ] ]; $VAR1 = [ '1', '2', '3', '4' ];
    illustrating the difference quite nicely.

      It can also be useful to name the variables using Dump() or Dumpxs() (see Data::Dumper) to avoid confusion.

      knoppix@Microknoppix:~$ perl -MData::Dumper -E ' > @arr1 = ( 1 .. 4 ); > @arr2 = [ 1 .. 4 ]; > $arr3 = [ 1 .. 4 ]; > print Data::Dumper->Dumpxs( > [ \ @arr1, \ @arr2, $arr3 ], > [ qw{ *arr1 *arr2 arr3 } ], > );' @arr1 = ( 1, 2, 3, 4 ); @arr2 = ( [ 1, 2, 3, 4 ] ); $arr3 = [ 1, 2, 3, 4 ]; knoppix@Microknoppix:~$

      I hope this is of interest.

      Cheers,

      JohnGG

Re: difference between @, $
by toolic (Chancellor) on Dec 07, 2011 at 17:06 UTC
Re: difference between @, $
by TJPride (Pilgrim) on Dec 07, 2011 at 21:30 UTC
    @a is an array, or potentially a set of values, while $a is a scalar, or single value. In this case, $a is being used to hold a reference to an array, but the reference is still a single value. I think you probably intended to assign the array values in list context, rather than just adding a reference:

    @a = ['1','2','3','4']; ### NO @a = ('1','2','3','4'); ### YES print $_ for @a; ### Prints 1234 print $a[0]; ### Prints 1 ### Note that you use $ and not @ when referring to a ### single item in the array $a = ['1','2','3','4']; print $_ for @$a; ### Prints 1234 print $a->[0]; ### Prints 1 ### The -> is used when it's a reference to an array

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-27 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls