CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Pushing @array with $num



Sep 21, 2003, 8:40 AM

Post #1 of 3 (1159 views)
Pushing @array with $num Can't Post

I need to be able to create an array via push, which also has an unknown number in it:

push(@array.$number, "$info");
But that fails.
I've also tried push(@array$number,$x); and
push ($name, $x);

Any ideas?
Also i will need to be able to do the same thing for foreach.
foreach $thing (@array.$num)


Thaumaturge / Moderator

Sep 21, 2003, 10:09 AM

Post #2 of 3 (1157 views)
Re: [andy7t] Pushing @array with $num [In reply to] Can't Post

Creating a variable whose name is created from another variable (as you want to do here) is known as using "symbolic references". Whilst Perl can do this it's generally considered a very bad idea. In fact it's such a bad idea that when you add "use strict" to your program (and you should _always_ have "use strict" in your programs) then symbolic references become illegal.

Mark Jason Dominus has written three good articles explaining exactly why you shouldn't do this.

I suggest that you use a hash of arrays to solve your problem instead.

push @{$hash{$number}}, $x;

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks


Sep 21, 2003, 10:53 AM

Post #3 of 3 (1154 views)
Re: [davorg] Pushing @array with $num [In reply to] Can't Post

The other question is how would i then do the same for foreach?
I've tried foreach $day (@day.$daynumber)
and foreach $day(@day$daynumber)

but i can't seem to get them to work.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives