Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

[@$]

Posted on 1998-07-17
2
Medium Priority
?
124 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

660 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