Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 8th, 2013, 09:22 AM
MA1985 MA1985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 MA1985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 48 sec
Reputation Power: 0
References Array

Hello All
I need to create an array with references while each cell of the array pointing to his own array in a way - so I can loop this reference array get into the 5 arrays that he pointing on and push the same lines into the different arrays. I tried to do something like this:

my(@arrA);
my(@arrB);
my(@arrC);
my(@arrD);
my(@arrE);

my($ref_to_arrA) = \@arrA;
my($ref_to_arrB) = \@arrB;
my($ref_to_arrC) = \@arrC;
my($ref_to_arrD) = \@arrD;
my($ref_to_arrE) = \@arrE;

@Ref_to_Arr = {$ref_to_arrA, $ref_to_arrB, $ref_to_arrC, $ref_to_arrD, $ref_to_arrE} # I initial the reference array with a pointers to my 5 arrays

my($moo);
foreach $moo (@Ref_to_Arr){
push(@Ref_to_Arr, "HELLO WORLD! ");
} #I want to go all over the Ref_to_Arr array and with the help of the references to push my string into my 5 arrays - @arrA @arrB @arrC @arrD @arrE


Now I expect, that if I will print the content of @arrA @arrB @arrC @arrD @arrE so I will see in every array the string: HELLO WORLD...

But something with the syntax is wrong...

Glad if you know how I can make it work...

Thanks a lot in advance!!

Reply With Quote
  #2  
Old January 8th, 2013, 09:38 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
Your basic problem is that you've used the wrong type of brackets for creating your array of references. Curly brackets (braces) will create a reference to a hash. You want round brackets (parentheses) to create an array, like so:
Code:
my(@arrA);
my(@arrB);
my(@arrC);
my(@arrD);
my(@arrE);

my($ref_to_arrA) = \@arrA;
my($ref_to_arrB) = \@arrB;
my($ref_to_arrC) = \@arrC;
my($ref_to_arrD) = \@arrD;
my($ref_to_arrE) = \@arrE;

my @Ref_to_Arr = ($ref_to_arrA, $ref_to_arrB, $ref_to_arrC, $ref_to_arrD, $ref_to_arrE)

Reply With Quote
  #3  
Old January 8th, 2013, 09:49 AM
MA1985 MA1985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 MA1985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 48 sec
Reputation Power: 0
Hi

Thx for the answer ! it really was a syntex error but even after I corrected the prentsis it still dont work. I think that it gets into an infinite loop maybe it something in the foreach and push section that I did wrong?

Thanks again!

Reply With Quote
  #4  
Old January 8th, 2013, 10:05 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 15 sec
Reputation Power: 1170
You're pushing the new value onto the wrong array.

Those initial array declarations aren't needed.

Code:
# the choice of var names could be improved
my $ref_to_arrA = [];
my $ref_to_arrB = [];
my $ref_to_arrC = [];
my $ref_to_arrD = [];
my $ref_to_arrE = [];

@Ref_to_Arr = ($ref_to_arrA, $ref_to_arrB, $ref_to_arrC, $ref_to_arrD, $ref_to_arrE);

foreach my $moo (@Ref_to_Arr){
    push(@{$moo}, "HELLO WORLD! ");
}

Reply With Quote
  #5  
Old January 8th, 2013, 10:24 AM
MA1985 MA1985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 MA1985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 48 sec
Reputation Power: 0
Hi,

Thanks a lot but still not working.
I did what you said and it gave me this error for the line that starts with "push..":

Scalar found where operator expected at HelloWorld.pl line 138, near "@($moo"
(Missing operator before $moo?)
syntax error at HelloWorld.pl line 138, near "@($moo"
Execution of HelloWorld.pl aborted due to compilation errors.

Do you know what this error means?

Reply With Quote
  #6  
Old January 8th, 2013, 10:39 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 15 sec
Reputation Power: 1170
You didn't copy the code correctly for the push statement.

You used ($moo) parens around the var where you should have used {$moo} braces.

Reply With Quote
  #7  
Old January 8th, 2013, 10:54 AM
MA1985 MA1985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 6 MA1985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 48 sec
Reputation Power: 0
WOW!! It works!!!! Thank you very much!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > References Array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap