Solved

[@$]

Posted on 1998-07-17
2
120 Views
Last Modified: 2010-03-05
I'm reading a manual on sybperl, a sybase module for perl. One function returns a reference to a static array (a row from the database) and this reference is stored in $d.
The manual says that to copy the array into a list of arrays, I need to do something like:

push(@rows, [@$d]) ;

My understanding is that the @ dereferences $d, while the [] create a reference to the list.
How and why does the procedure above create a copy of the array that $d references instead of just referencing the same thing? In other words, why is [@$d] not the same thing as $d?
Thanks,

Robert
0
Comment
Question by:lendvai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Expert Comment

by:alamo
ID: 1208489
As I understand it, the [] produces a reference to an anonymous array, not the original array. Compare that to \@$d which I think would be equivalent to $d.
0
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1208490
Basically, [@$d] makes a copy of the array referenced by $d, and returns a reference to it.

A good way of playing with these things is to use the perl debugger:


(frev40)/u/[4]%perl -dw
 
Loading DB routines from perl5db.pl version 1.01
Emacs support available.
 
Enter h or `h h' for help.
 
use strict;
 
my(@a, $d, @rows);
@a = (1,2,3);
$d = \@a;
push(@rows, $d); # Puts the scalar $d (a reference to @a) on @rows
push(@rows, @$d); # puts the values in $d onto @rows);
push(@rows, [@$d]); #puts a reference to a copy of $d on @rows
 
$d->[0] = 10;
 
print "All Done\n";
^D [^Z for Win32]
 
main::(-:3):    my(@a, $d, @rows);
  DB<1> n
main::(-:4):    @a = (1,2,3);
  DB<1> n
main::(-:5):    $d = \@a;
  DB<1> n
main::(-:6):    push(@rows, $d); # Puts the scalar $d (a reference to @a) on @rows
  DB<1> n
main::(-:7):    push(@rows, @$d); # puts the values in $d onto @rows);
  DB<1> n
main::(-:8):    push(@rows, [@$d]); #puts a reference to a copy of $d on @rows
  DB<1> n
main::(-:10):   $d->[0] = 10;
  DB<1> n
main::(-:12):   print "All Done\n";
  DB<1> x @rows
0  ARRAY(0x18c924)
   0  10
   1  2
   2  3
1  1
2  2
3  3
4  ARRAY(0xb3948)
   0  1
   1  2
   2  3
  DB<2> q


Shows the difference pretty well... By the way, I generally recommend that people not use sybperl, and go directly to using DBlib ...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question