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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Changing the name of an array

 



encsteph
Deleted

Aug 14, 2000, 7:56 AM

Post #1 of 4 (503 views)
Changing the name of an array Can't Post

I am trying to get this little chunk of code to work but keep getting an error. I am unable "@special_$count" to work not matter how I try it. Can somebody tell me how I can increment the array so I can create an unkown amount of arrays when it reads this file in?

open (PCFILE, "flat.txt");
@cat = <PCFILE>;
close PCFILE;


my $count = 0;
foreach $category (@cat) {

if ($category =~ /^01/) {
$count++;
}

push (my @special_$count, $_);

}



encsteph
Deleted

Aug 13, 2000, 10:24 PM

Post #2 of 4 (503 views)
Re: Changing the name of an array [In reply to] Can't Post

I found out that $count is not allowed in the naming of an array. That is the problem though, I am trying to set this up so that each time it loops through it pushes the contents of $_ into @array_$count. Then it changes the array based on matching the if statement, so that $_ is then pushed into the next array and not the same one.


dws
Deleted

Aug 14, 2000, 9:32 AM

Post #3 of 4 (503 views)
Re: Changing the name of an array [In reply to] Can't Post

'$' isn't legal in an identifier. Try renaming @special_$count to @special_dollar_count.


Kanji
User

Aug 14, 2000, 1:57 PM

Post #4 of 4 (503 views)
Re: Changing the name of an array [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

push @{ "special_$count" }, $_;</pre><HR></BLOCKQUOTE>
...will create @special_0, @special_1, etc. IMHO, you'd be better off going with an array of arrays...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my @special;
my $count = 0;
foreach my $cat (@cat) {
$count++ if ($category =~ /^01/);

push @{ $special[$count] }, $_;
}</pre><HR></BLOCKQUOTE>
...which is functionally equivalent to creating the following...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@special = (
[ 1, 2, 3 ], # @{ $special[0] }
[ 3, 4, 5 ], # @{ $special[1] }
[ 6, 7, 8 ], # @{ $special[2] }
);</pre><HR></BLOCKQUOTE>
Then you could do print @{ $special[2] }; and get 6, 7, & 8, while print $special[0][2]; would get 3.


 
 


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

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