For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > array assignement









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author array assignement
Jeevs

2007-07-23, 6:59 pm

Hi forum!

I just wanted to know what does the following line do....
@{$args{owner}} = qw(hero wierd);

lets assume $args{owner} = 'sachin';
Then it would mean @{sachin} = qw(hero wierd);
what would {sachin} stand for does it mean an hash refernce or
something else. I am lost.

Can someone explain or atleast point me to some documentation, I tried
looking into the perldoc but i am sure i missed something.

John W. Krahn

2007-07-23, 6:59 pm

jeevs wrote:
> Hi forum!


Hello,

> I just wanted to know what does the following line do....
> @{$args{owner}} = qw(hero wierd);


You are assigning a list to the anonymous array in $args{owner}.


> lets assume $args{owner} = 'sachin';


'sachin' is a scalar value.


> Then it would mean @{sachin} = qw(hero wierd);


No, the scalar value is replaced with an anonymous array.


> what would {sachin} stand for does it mean an hash refernce or
> something else. I am lost.


'sachin' would not exist after the assignment.


> Can someone explain or atleast point me to some documentation, I tried
> looking into the perldoc but i am sure i missed something.


perldoc perldsc
perldoc perllol
perldoc perldata
perldoc perlreftut
perldoc perlref



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
John W. Krahn

2007-07-23, 6:59 pm

John W. Krahn wrote:
> jeevs wrote:
>
> You are assigning a list to the anonymous array in $args{owner}.
>
>
>
> 'sachin' is a scalar value.
>
>
>
> No, the scalar value is replaced with an anonymous array.
>
>
>
> 'sachin' would not exist after the assignment.


Correction, the assignment wouldn't happen:

$ perl -le'
use Data::Dumper;
my %args;
@{ $args{ owner } } = qw( hero wierd );
print Dumper \%args;
$args{ owner } = q/sachin/;
print Dumper \%args;
@{ $args{ owner } } = qw( hero wierd );
print Dumper \%args;
'
$VAR1 = {
'owner' => [
'hero',
'wierd'
]
};

$VAR1 = {
'owner' => 'sachin'
};

$VAR1 = {
'owner' => 'sachin'
};

It would work if you assigned the anonymous array directly:

$ perl -le'
use Data::Dumper;
my %args;
$args{ owner } = [ qw( hero wierd ) ];
print Dumper \%args;
$args{ owner } = q/sachin/;
print Dumper \%args;
$args{ owner } = [ qw( hero wierd ) ];
print Dumper \%args;
'
$VAR1 = {
'owner' => [
'hero',
'wierd'
]
};

$VAR1 = {
'owner' => 'sachin'
};

$VAR1 = {
'owner' => [
'hero',
'wierd'
]
};



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Mr. Shawn H. Corey

2007-07-23, 6:59 pm

John W. Krahn wrote:

> $ perl -le'
> use Data::Dumper;
> my %args;
> @{ $args{ owner } } = qw( hero wierd );
> print Dumper \%args;
> $args{ owner } = q/sachin/;
> print Dumper \%args;
> @{ $args{ owner } } = qw( hero wierd );
> print Dumper \%args;


print Dumper \@sachin;


Of course, if you `use strict;` this will fail. The preferred method is to place it in a hash:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %args = ( owner => 'sachin' );
my %hash = ();
@{ $hash{$args{owner}} } = qw(hero wierd);

print "\%hash: ", Dumper \%hash;

__END__


--
Just my 0.00000002 million dollars worth,
Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
Paul Lalli

2007-07-24, 6:59 pm

On Jul 23, 4:35 am, jeevan.ing...@gmail.com (Jeevs) wrote:

> I just wanted to know what does the following line do....
> @{$args{owner}} = qw(hero wierd);
>
> lets assume $args{owner} = 'sachin';
> Then it would mean @{sachin} = qw(hero wierd);
> what would {sachin} stand for does it mean an hash refernce or
> something else. I am lost.


This is known as a symbolic reference, and is a very very bad idea,
for exactly these reasons. `use strict;` prevents you from doing
messy stuff like this. You should always use strict. If you do not:

perl -MData::Dumper -le'
$args{owner} = "sachin";
@{$args{owner}} = qw(hero wierd);
print Dumper(\%args);
print Dumper(\@sachin);
'

$VAR1 = {
'owner' => 'sachin'
};

$VAR1 = [
'hero',
'wierd'
];

> Can someone explain or atleast point me to some documentation, I tried
> looking into the perldoc but i am sure i missed something.


perldoc perlref
perldoc perlreftut

Basically, when you use a string as though it was a reference, as you
did above, you create a "symbolic reference". You are modifying the
variable named by that string. So if $args{owner} contains the string
'sachin', then @{$args{$owner}} is the same thing as the array
variable @sachin.

Some people try to take advantage of this apparent feature by using it
to dynamically choose which variables they want to access. To learn
the correct way of going about that, please read:
perldoc -q "variable name"

Paul Lalli


Paul Lalli

2007-07-24, 6:59 pm

On Jul 23, 1:51 pm, kra...@telus.net (John W. Krahn) wrote:
> John W. Krahn wrote:
>
>
>
>
>
>
>
>
>
> Correction, the assignment wouldn't happen:


No, the assignment happens just fine. It's just not assigning to what
you think it's assigning to.

The OP was using symrefs. By referring to a string as though it was a
reference, he modified an unrelated variable.

>
> $ perl -le'
> use Data::Dumper;
> my %args;
> @{ $args{ owner } } = qw( hero wierd );


This sets $args{owner} to be a reference to an anonymous array
containing('hero', 'wierd');

> print Dumper \%args;
> $args{ owner } = q/sachin/;


This sets $args{owner} to be the string 'sachin';

> print Dumper \%args;
> @{ $args{ owner } } = qw( hero wierd );


This sets the array @sachin to contain ('hero', 'wierd');

It has nothing whatsoever to do with %args.


Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com