Richard Heintze wrote:
Wiggins and Jan,
Thank you very much for such an extremely prompt
response.
You are right, I want
my @PCEs = ();
I'm using the Win32 ODBC API on acres of legacy code that does not "use strict" or "use warnings". I thought the Win32 ODBC API was compatible with DBI but according to your comments, I'm nonstandard. I guess I should try to be compliant. I thought I read that the Win32 ODBC API was a subset of DBI. I guess I'm incorrect.
Legacy code is a bear, obviously sometimes it can't be helped and just adding strict/warnings won't work, but you should get into the habit of writing new code and your changes so that eventually they can be added once all the legacy code is updated. As for the ODBC API, I am not familar with it, so your code may be correct, but the DBI itself does not have the two methods you used, which isn't necessarily a problem, but you need to make sure to provide us enough info so that we know what you are up to. It seems very odd that FetchRow would be called in a boolean context but maybe it is (read: I would hate to have a method called 'Fetch' anything and not store the value it returns, eck). I don't know if it is a subset of DBI but I sort of doubt it since it has different method names.
I'm working on a cgi page called "Administer Cases". I just explained to my client that the labor costs are
three or four times what they should be on account of
the fact that this page is in desperate need of a
rewrite. He said no, patch it up some more. Oh well.
Yep, sounds about right. Just make sure you charge him what you should... if he wants to pay it, then by all means let him.
So if say '$PCEs[0]{"xyz"} = 44;' is there any way I can make '$PCEs[1] = "abc";' generate a syntax or runtime error because I forgot the extra "{}" (assuming I could turn on "use strict; use warnings;").
Not a syntax error, but if it is in a loop or you are calling a sub with the value or something then you could add an extra check using the 'ref' method.
perldoc -f ref
But then you just have more code in there to look for bugs, rather than handling the business logic and fixing the bugs. Type issues should either be fixed in the interface by checking values passed to encapsulated code, or in the main by testing. It should be very apparent at runtime if you are assigning a scalar where a hashref was expected.
http://danconia.org
--- Wiggins d'Anconia <[EMAIL PROTECTED]> wrote:
Richard Heintze wrote:
Is there a way I can explictly declare that each
array
cell contains a hash?
Only with a loop of some sort.
Here is the only way I know to do it:
my @PCEs=[];
This does not do what you think it does. This is
assigning an anonymous array reference into the first element of the array.
while ($Data->FetchRow()) {
What is 'FetchRow' doing? This is not normal DBI,
and is being used in a true/false context? You also don't need the empty
parens, but that is more stylistic.
my %dh = $Data->DataHash(); $PCEs[$cn]{$dh{"id"}} = $dh{"ridPCE"};
Where did $cn come from? Are you using strict and
warnings? If not you need to be. $cn is not changing in your loop so
while FetchRow is true you are going to continually overwrite the value you
are assigning which will cause your array to have one value, the last.
If you are changing $cn in the DataHash method you shouldn't be and have
broken the encapsulation, but that is a design issue.
}
This "my" declaration only says that it is an
array,
not an array of hashes. Can I improve upon this?
'my' is for defining scope and nothing more. It is
how you use the variable that determines what it contains. Your
assignment statement will autovivify the element in the array into a hash
reference as you want so there is no need to improve it. If you want
to force an array element into a hash reference then something like,
$PCEs[$cn] = {};
Will set the element of PCEs at index $cn to an
empty hash reference, but isn't necessary if you use the variable
initially as above.
perldoc -f my perldoc perlreftut perldoc perlref perldoc perllol perldoc perldsc
You should read through the docs above, it will help
you understand complex data structures in Perl. You may also want
to look at,
perldoc Data::Dumper
To help you visualize what your data structures contain. And ALWAYS do,
use strict; use warnings;
At the top of your code....
http://danconia.org
__________________________________ Do you Yahoo!? Yahoo! Finance: Get your refund fast by filing online. http://taxes.yahoo.com/filing.html
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>