<forwarded to the list as i was emailed off list. no reason to keep this
private IMO>
On 05/24/2017 08:20 PM, lee wrote:
Uri Guttman <u...@stemsystems.com> writes:
you can get an array of hashes, where each hash is one row.
learning dereferencing will do you much more good than using eval
EXPR. references are key to deeper data structures in perl and are
used all over the place. it is a core skill you should focus on as it
will solve your current problem and many to come.
Since when are there any data structures in perl? Variables are even
virtually typeless.
since when is a variable a data structure? :)
the key is again references which still seems to confuse you. a scalar
can hold only one thing. a array a list of things. a hash can hold
pairs of keys/values. but most real world data is made of combinations
of those things. an array of hashes is like a list of records. a hash of
hashes can be keyed by names and then each subhash has data about its
name. you can't make those without references.
if you need help with references, this list is the place to ask.
Why is it so awfully difficult to dereference a reference to an array
that contains references to arrays, and why does DBI, for example,
provide a function that returns such a reference? It involves holding
the data in memory, so what is the supposed advantage of using cascaded
references?
how will you organize the data without references? they are the branches
in the tree of data. returning a single reference is much more efficient
than returning the whole array or hash. that would involve copying the
array/hash onto the stack and then copying it again to a variable in the
caller. that is very slow.
you say cascaded references. i call that a data tree. it is incredibly
valuable and used all over the place. as i said above most real world
data is more complex than what a single hash or array can hold. think
about something like what is needed to manage an editor or word
processor. they need complex data trees (usually in c/c++ but the same
idea). so references aren't an evil to be handled but the key to better
management of data. any single slot in an array or hash can hold another
array or hash via a reference. that is building up data. a phrase i use
is compleity arises from layers of simplicity. a single hash is simple,
a large data tree which is made from simple hashes can be very complex.
references are how that works.
The program(mer) will want to work with the data rather than with
cascaded references to it, and in the unlikely case cascaded references
would be wanted, they could still be created. The references are merely
an obstacle.
then how will you organize that data without references? think about it
for a minute. there is no good answer to that question. if you think
refs are obstacles, you have plenty to learn. data is complex so they
they complex structures. life can't live in single hashes.
They are also inherently dangerous because it is never obvious if some
variable is a reference to something or not, and that variables are
virtually typeless contributes to that. Otherwise, references could be
helpful for passing data to functions.
it is easy to tell. use the ref function. and if you design your
structures cleanly, you generally know when a value should be a ref or a
regular value. if you have to have either type, checking with ref will
do the trick.
For example:
my $foo;
Is that a reference? An integer? A float? A vector of characters? A
data structure? A character? Something else? There is no way to
tell. Pass it to a function and you have to ask again if it's a
reference or a variable.
perldoc -f ref
and you rarely need to know so many different types of values in one scalar.
Why don't references at least require a different notation? An 'int
*foo;' tells you right away what it is.
typedef struct {
unsigned long MemTotal;
unsigned long MemFree;
unsigned long Buffers;
} meminfo;
so use hungarian notation in the name. $foo_ref. not what i would do but
it works for that.
... also tells you what it is, and when you try to misuse it, you get an
error message. It is impossible to create variables of self-defined
types in perl because the variables don't have types to begin with.
That includes data structures.
this is perl. it is designed to allow anything to be stored in a slot.
you said it earlier, it is not strongly typed which is a win for many
things. an array can hold different value types.
and in c you don't get deep structures without pointers and that has the
same issues you seem to find with references.
My limit with "data structures" in perl are strided lists. They are a
poor clutch and I don't like them, but unfortunately, there are no data
structures in perl.
you are wrong about no data structures in perl. you will have to take my
word on that (and everyone else who teaches perl). you just haven't
learned references yet. once you do, it will all make more sense.
They are potentially dangerous because you might fetch huge numbers of
rows and run into memory issues which could bring down the server. I'm
avoiding them unless they have a significant advantage and when I can be
sure not to fetch too many rows, or when I would have all the data in
the program anyway.
you can get a row at a time i am sure with each row being a hash.
The hash would have to be transformed into an array. Hashes are
difficult to debug because you don't get any error when using elements
that do not exist, so they are as inherently dangerous as references are
--- or even more dangerous because you sometimes get error messages
telling you that something cannot be used as reference to a an array.
there are ways to lock hash keys so look for that. i have yet to need
that and don't run into that problem much.
it all comes down to experience in anything like this. you haven't used
references enough to really get them so they seem nasty to you. i can
only say you need to persevere with them. you are welcome to show code
that bothers you and ask questions here. but ranting on how references
are evil is not productive anywhere.
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/