how to name an array such that it can include a variable (for incrementing)

J

Jonas Yorg

Hi, I am looping through a file and cutting chunks of it out each to
their own new array for later parsing. I want to have the arrays
names like
@array1
@array2
@array3
etc

so I had a type of counter var $n which I wanted to use like

@array$n = ();

and then $n++; when I am done with that section of data

but that doesn't seem to work...can anyone give me a hint on how I can
make these sort of incrementing arrays?


thanks

Jonas
 
P

Peter Cooper

Jonas said:
<snip>
can anyone give me a hint on how I can make these sort of
incrementing arrays?

Hi Jonas,

You can create 'arrays within arrays'. These are often called 'two
dimensional' arrays. Try this code and see if it makes sense:

my @bigarray;
foreach my $index (0..9) {
$bigarray[$index][0] = "test";
$bigarray[$index][1] = "blah";
$bigarray[$index][2] = "and so on";
}
# Access array 7, element 1
print $bigarray[7][1];

You can extrapolate the 'internal' arrays, by using, say, $bigarray[7] as an
/array reference/, like so:

# Loop all inner elements of array 7
foreach $data (@{$bigarray[7]}) {
print $data;
}

Cheers,
Pete
 
B

bd

Jonas said:
Hi, I am looping through a file and cutting chunks of it out each to
their own new array for later parsing. I want to have the arrays
names like
@array1
@array2
@array3
etc

so I had a type of counter var $n which I wanted to use like

@array$n = ();

and then $n++; when I am done with that section of data

but that doesn't seem to work...can anyone give me a hint on how I can
make these sort of incrementing arrays?

You can do
@{"array$n"} = ();
But you shouldn't. Use references instead:
$arrays[$n] = [];

See perldoc perlreftut for more info
 
D

David K. Wall

[symbolic references]
People programming in Java, C or Python don't ask this, and they
probably don't need it either. Why do so many (newbie?) Perl programmers
want this. Why does this strike them as a good programming practise?

Maybe a reason I tried to use symbolic references at first was because the
"language" I used most before Perl was SAS (http://www.sas.com). SAS macros
don't allow anything *but* symbolic references. All you have is string
variables. That's not much of a handicap when your main use of SAS is
using canned procedures for standard statistical analyses.

But I had programmed a little (not much) in Basic, Fortran, Pascal, C, and
just touched on awk before using Perl, so perhaps that's not a real excuse.

It may be that variable interpolation in double-quoted strings is the
culprit. When you can type

print "The variable contains $string_or_number";

and have it work as expected (for certain values of "expected"), it's not
too much of a jump to try the same thing with variable names. At least,
that's how I seem to recall thinking about it. How many other languages
even allow you to do this?
 
J

Jonas Yorg

Abigail said:
Jonas Yorg ([email protected]) wrote on MMMDCXLV September MCMXCIII in
<URL::: Hi, I am looping through a file and cutting chunks of it out each to
:: their own new array for later parsing. I want to have the arrays
:: names like
:: @array1
:: @array2
:: @array3
:: etc
::
:: so I had a type of counter var $n which I wanted to use like
::
:: @array$n = ();
::
:: and then $n++; when I am done with that section of data
::
:: but that doesn't seem to work...can anyone give me a hint on how I can
:: make these sort of incrementing arrays?


I will tell you how if you tell me why you want this. It's a question
that's asked over and over again (and answered over and over again),
but noone who asks this question ever explains why they want this.

People programming in Java, C or Python don't ask this, and they
probably don't need it either. Why do so many (newbie?) Perl programmers
want this. Why does this strike them as a good programming practise?



Abigail


why do I want to do this? it's simple, I'm hacking a text file into a
bunch of little sections each of which requires its own processing
AFTER it's been seperated out. I am working on a bunch of files that
have SIMILAR but not exactly the same format and thus I don't know how
many new chunks I'm going to be coming up with. So I need a bunch of
arrays to have all different names and I want to be able to have
created them in an orderly fashion(i.e. using a counter to give each
one a number on the end of it) so that I can then later loop back
through and deal with each of them in an orderly fashion. Yes I'm a
newbie to perl and my ways are probably not the most efficient but I'm
not the kind of computer nerd who cares about good coding style, I'm
not the kind of computer nerd who even likes to program all that much,
I'm the kind of computer nerd who cares about security and not much
else. Yes coding directly translates to security, but that's not
relevant for some script I'm throwing together and that's throwing me
off onto a tangent.

that's why I want to do it Abigail, but others have already been more
helpful anyway.
 
J

Jonas Yorg

Peter Cooper said:
Jonas said:
<snip>
can anyone give me a hint on how I can make these sort of
incrementing arrays?

Hi Jonas,

You can create 'arrays within arrays'. These are often called 'two
dimensional' arrays. Try this code and see if it makes sense:

my @bigarray;
foreach my $index (0..9) {
$bigarray[$index][0] = "test";
$bigarray[$index][1] = "blah";
$bigarray[$index][2] = "and so on";
}
# Access array 7, element 1
print $bigarray[7][1];

You can extrapolate the 'internal' arrays, by using, say, $bigarray[7] as an
/array reference/, like so:

# Loop all inner elements of array 7
foreach $data (@{$bigarray[7]}) {
print $data;
}

Cheers,
Pete


yeah, I had thought of using multi-dimensional in the first place but
then I wanted to know if it was possible to do more like what I was
talking about. But that will work. Thanks!
 
T

Tad McClellan

Jonas Yorg said:
why do I want to do this?
So I need a bunch of ^^^^
arrays to have all different names and I want to be able to have
created them in an orderly fashion


You have switched from "want" to "need".

You do not "need" that, you can use a multi-dim data structure
as a safer alternative.

Yes I'm a
newbie to perl and my ways are probably not the most efficient


Avoiding symrefs is not for efficiency reasons.

It is to avoid security problems and hard-to-find bugs.

I'm the kind of computer nerd who cares about security and not much
else.


Then do not use symrefs, use real references instead.

that's why I want to do it Abigail, but others have already been more
helpful anyway.


If Abigail has helped you avoid using symbolic references when you
do not need to use them, then she _was_ helpful, whether you can
see it or not.
 
J

JS Bangs

Jonas Yorg sikyal:

Are you sure about this? I mean, have you hung out on the C and Python
newsgroups and observed the lack of questions regarding this topic? (I
haven't, so this is a non-rhetorical question.) I do know that this is a
common question--sort of--on the PHP newsgroups. It's much more easily
answered in PHP, though, since there is no broad censure against it, the
syntax is easier (usually just $$foo), and there are no hard references to
be used instead. These are not advantages of PHP, by the way, simply
reasons why this question comes up less frequently on that newsgroup.

Perhaps a personal example will help: when I first learned Perl, I was
learning it more or less as my first programming language, building off of
a fragmentary and inadequate knowledge of BASIC that I'd picked up from
somewhere. Before long, I wanted to use symbolic references, and my
reasons weren't dissimilar to the ones that the OP mentions: I thought
they were more elegant. Rather than creating a separate hash that would
serve no purpose other than to hold unconnected bits of data that I really
wanted, it seemed more concise and elegant to make a variable with a name
found in another variable.

These were my main reasons: conciseness and elegance. These are generally
good programming traits. The problem is that in this case, the perceived
benefit in elegance is outweighed by much larger costs in terms of
security and predictability. So the newbies that ask these questions
aren't completely clueless, as much as undereducated about the negative
effects of symbolic references.

--
Jesse S. Bangs (e-mail address removed)
http://students.washington.edu/jaspax/
http://students.washington.edu/jaspax/blog

Jesus asked them, "Who do you say that I am?"

And they answered, "You are the eschatological manifestation of the ground
of our being, the kerygma in which we find the ultimate meaning of our
interpersonal relationship."

And Jesus said, "What?"
 
A

Alan J. Flavell


[at least two applications for fast-track entry into the killfile]
You have switched from "want" to "need".

....and from "I don't understand" to "please plonk me".
Avoiding symrefs is not for efficiency reasons.
right

Then the hon usenaut should pay attention.

Till then...


--
Mag sein. Aber ich seh da _gar kein_ Menue.
Weil es zugeklappt ist.
 
J

Jonas Yorg

You have switched from "want" to "need".

You do not "need" that, you can use a multi-dim data structure
as a safer alternative.




Avoiding symrefs is not for efficiency reasons.

It is to avoid security problems and hard-to-find bugs.




Then do not use symrefs, use real references instead.




If Abigail has helped you avoid using symbolic references when you
do not need to use them, then she _was_ helpful, whether you can
see it or not.

Abigail didn't do anything, someone else posted a solution that was
usable, he/she/it did not
 
J

Jonas Yorg

JY> that's why I want to do it Abigail, but others have already been more
JY> helpful anyway.

and you still want to do it the wrong way? then they didn't help enough.
no, the people who are actually helpful gave me a good way to do it,
where did I ever say that I was still doing it that way? I was merely
saying that it's pointless to pretend to help when other people have
already given solutions.

You are suffering from the same impedement, you are posting nothing
that helps, and you're not even really giving new information, you're
just trying to feel superior which is fine because I am just learning
perl and I don't care that you know more about it than me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top