Query related to references in array in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Query related to references in array in Perl
# 1  
Old 09-13-2012
Query related to references in array in Perl

Hi All
I have a doubt and want to be cleared I am using
Code:
@array = (10, 20);
$rarray = \@array;
#print "$rarray\n";
#print "@$rarray\n";

 $rr=  \$array[0];
 #print $$rr;
 $rr++;
 print $$rr;

As you can see the $rr contains the reference to the first element of the array , now as the definition say array contains the continuous memory location, so when I incremented the $rr with 1 so it should now print the value for $array[1]..
Please correct me if I am wrong.

---------- Post updated at 04:52 AM ---------- Previous update was at 03:02 AM ----------

Any Idea
# 2  
Old 09-13-2012
You cannot do $rr++ in perl like the way you can in C.
Perl offers a different way of accessing the subsequent elements of an array through reference variables. If you didn't already know this, take a look at the sample below:

For e.g.,
Code:
#! /usr/bin/perl -w
use strict;

my @array = (10, 20);
my $rarray = \@array;
print "@{ $rarray }[0]\n";
print "@{ $rarray }[1]\n";

# 3  
Old 09-13-2012
Also one more question , what does this Code mean
Code:
@row = (1,2,3,4);

push(@{check}, \@row);

If I want to print the values then how can I??
Also how can get the reference value for @row

Last edited by parthmittal2007; 09-13-2012 at 12:23 PM.. Reason: Addition of data
# 4  
Old 09-13-2012
Quote:
Originally Posted by parthmittal2007
Also one more question , what does this Code mean
Code:
@row = (1,2,3,4);

push(@{check}, \@row);

push (@check, \@row) : Push the reference of @row into @check.

Quote:
If I want to print the values then how can I??
Print the values of what?

Quote:
Also how can get the reference value for @row
\@row
# 5  
Old 09-14-2012
Code:
@row = (1,2,3,4);
push(@{check}, \@row);
print ${$check[0]}[1]."\n"; #1
print $check[0][1]."\n"; #2
print $check[0]->[1]."\n"; #3

I mean to say which option (1 OR 2 OR 3) is correct??
If option 2 is correct , then why??
I am not getting it , please explain

---------- Post updated at 11:05 PM ---------- Previous update was at 01:02 PM ----------

Any idea
# 6  
Old 09-14-2012
All three options are different ways that perl offers you to access the same element. Remember, with perl, its always TMTOWTDI. You adopt the style that you're comfortable with.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Interfaces related query

I need to list the interfaces that uses FTP instead of SFTP on my applications that are on AIX servers. How do I get that list of IP addresses that connect to my applications via FTP? (2 Replies)
Discussion started by: ggayathri
2 Replies

2. UNIX for Dummies Questions & Answers

query related to grep

Hi All, The result for 'grep "cert_codes" /develop/sales/appl.srce/*.4gl' command will be saved at aa.txt grep "cert_codes" /develop/sales/appl.srce/*.4gl >aa.txt But I am not sure, whether, all result stored in .txt file in case of multi-line result. Please revert back if... (2 Replies)
Discussion started by: pbankar
2 Replies

3. UNIX for Dummies Questions & Answers

A query related to 'ls' command

I am executing the ls command to show the contents of a folder, it shows some number in front of word total as highlighted in blue color below quotes. Can anyone please share that what it is? (2 Replies)
Discussion started by: Zaib
2 Replies

4. Shell Programming and Scripting

Perl References/Dereferences

Can someone explain where can we actually used print $var->; or print $$var When does the -> becomes necessary and when its optional. (1 Reply)
Discussion started by: dinjo_jo
1 Replies

5. Shell Programming and Scripting

datetimestamp related query!

Hi all, I have put a query in a thread but didn't get any reply. Hoping to get a reply here. I have a file in that one line resembles like below... Forwarded by Deepak on 11/15/2009 10:28 AM EST ofcourse AM can be PM also... so what i need is first i need to get only... (1 Reply)
Discussion started by: smarty86
1 Replies

6. Shell Programming and Scripting

perl - passing hash references to functions

hi there I have the following script in which i have created a PrintHash() function. I want to pass to this function the reference to a hash (in the final code i will be passing different hashes to this print function hence the need for a function). I am getting an error Type of arg 1 to... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

7. Shell Programming and Scripting

query related to if

wht does below statement mean? if wht does dis -d option do?? TIA. (1 Reply)
Discussion started by: sainathdeg
1 Replies

8. UNIX for Dummies Questions & Answers

awk related query

hi, I have to extract a column from a file and then updated that column..?? Now i can use wak for extracting it and then how to update it.. $ awk' {print $5}' input_file Can i use sed command here piping it to the output from the awk command.. (2 Replies)
Discussion started by: abhisek.says
2 Replies

9. Shell Programming and Scripting

sed related query

Hi I have a file which looks like this //string = "abcd"; //info //string = "*pqrs"; //add string = "#123"; //sub //string = "#1234567890" data = check(string) //string = "#1234567890" I want to modify this as string = "#987"; //mult data = check(string) How do i do that? (1 Reply)
Discussion started by: gopsman
1 Replies

10. Shell Programming and Scripting

Query related to #!/bin/sh

hi All Why is #!/bin/sh being used in most of the ksh scripts......? I have seen this (#!/bin/sh) being used at the start of the script Regards Suresh (2 Replies)
Discussion started by: sureshg_sampat
2 Replies
Login or Register to Ask a Question