LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2005, 12:59 AM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
pointers in perl


hello,


i'm trying to reference an array in perl. I read alot of information on the internet about this, but i'm still having a problem for so reason,

he is my code:
Code:
#! /usr/bin/perl

$array[0] = 1;
$array[1] = 2;
$array[2] = 3;
print "array $array[0] \n";
@ref_array = \@array;
print "Dereference: $$ref_array[0]\n";
the output i get is
Code:
array 1
Dereference:
and nothing shows up for the reference (pointer) array, however, when i take out one of hte $ on the
Code:
 print "Dereference: $$ref_array[0]\n";
it prints out hte memory location, how can i dereference this array correctly?

thanks,

djgerbavor3
 
Old 04-10-2005, 01:17 AM   #2
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
nevermind, 2 minutes after i posted this, i saw what i was doing wrong:

when referencing arrays you need to do this:
Code:
$ref_array = \@array
INSTEAD OF
Code:
@ref_array = \@array
Sorry for the double post,

basically a good way to think about pointers or reference in perl is to think that pointers are a special kind of scalar.
So when initalizing a pointer you need create a "special scalar". However pointers can be of two things in perl. One is a regular reference like in C. and another kind of reference is an anonymous copy. An Anonymous is hte same as a regular reference but instead of indirectly referencing the value it makes a copies of value.
For example:
A reference declaration:
Code:
$ref_array = \@array or $ref_var = \$i;
anonymous copy is alittle different
Code:
$ref_array = [@array] ; like so wiht a scalar
and to deference your pointer you can do:
Code:
print "Deference $$ref_array[$i] \n";
this works both for reference and anonymous copies also.

hope this helps!!! this is a good way to create mult-dimenisonal arrays.. for more information about pointers and reference you can go here
http://linux.skavt.net/teach_perl_in_21days/ch18.htm

thanks,

djgerbavor3
 
Old 04-10-2005, 09:49 AM   #3
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
Thanks for the pointers djgerbavore!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
Pointers Pointers Pointers urzumph Programming 9 03-11-2004 09:49 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
need help with pointers qanopus Programming 8 02-03-2003 05:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration