ORA-06531:Reference to uninitialized collection - oracle12c

i have a following code in pl/sql anonymous block & getting error "Reference to uninitialized collection".
declare
myarray my_array_type := my_array_type ();
begin
PKG.GETVAL(myarray);
for I in 1..myarray.count
DBMS_OUTPUT.PUT_LINE('Value : '||myarray(I).value);
end;
PKG.GETVAL() returns some values in array.
Error report:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 4
06531. 00000 - "Reference to uninitialized collection"

Related

Perl: Type of argument to keys on reference must be unblessed hashref or arrayref

I have a Perl hash (from some legacy code) but am unable print out the the keys.
if (ref $val eq ref {}) {
print "Keys: " . keys $val . "\n";
e.g. here's the output I get:
VAL: HASH(0x7ff0898eda70)
Type of argument to keys on reference must be unblessed hashref or arrayref
I've read this Type of argument to keys on reference must be unblessed hashref or arrayref but not sure how to apply it in this case.
Is there a way of fixing this?
====
UPDATE
I've also tried:
print "Keys: " . keys %$val . "\n";
but still get Type of argument to keys on reference must be unblessed hashref or arrayref
UPDATE 2
I can see I have the key a_key but I'm unable to print out its values. E.g. debugging with Carp::REPL I get:
$ print $val;
1$ HASH(0x7fb1e0828f00)
$ print %$val;
1$ a_keyARRAY(0x7fb1e0828e28)
$ print %$val{'a_key'}
Compile error: syntax error at (eval 412) line 63, near "$val{"
BEGIN not safe after errors--compilation aborted at (eval 412) line 63, <FIN> line 6.
$ print $val{'a_key'}
Use of uninitialized value in print at (eval 413) line 63, <FIN> line 7.
1
UPDATE 3
Using Data::Dumper in the REPL I get:
$ print Dumper( $val );
$VAR1 = {
'a_key' => [
'long_value'
]
};
1$ print Dumper( %$val );
$VAR1 = 'a_key';
$VAR2 = [
'long_value'
];
1$ print %$val[1]
Compile error: syntax error at (eval 450) line 63, near "$val["
BEGIN not safe after errors--compilation aborted at (eval 450) line 63, <FIN> line 44.
$ print %$val{'a_key'}
Compile error: syntax error at (eval 451) line 63, near "$val{"
BEGIN not safe after errors--compilation aborted at (eval 451) line 63, <FIN> line 45.
$ print $val[1]
Use of uninitialized value in print at (eval 452) line 63, <FIN> line 46.
First of all, you have a precedence problem. You are doing
keys($val . "\n")
instead of
keys($val) . "\n"
Secondly, the syntax for keys[1] is
keys HASH
meaning you need
keys(%$val)
Finally, you are calling keys in scalar context, which returns the number of keys in the hash. Call it in list context to get the keys of the hash. For example,
say "Keys: ", join ", ", keys(%$val);
There were a few versions where Perl experimented with allowing keys $ref, but the experiment was discontinued in 5.24. Avoid that!

delete a key from a hash of array but I get Use of uninitialized value in hash element

I would like to know how to remove a key from a hash of array.
Given the following file as a simple example, forth column is always the third column in the paired match and data in not sorted.
....
NS501570 WWW 3009824 3009848
....
NS501572 WAD 3009848 3009898
....
I would like to collapse it into
NS501570 WWW 3009824 3009848 NS501572 WAD 3009848 3009898
I wrote the following code. I selected third column as key for hash. therefore I print the values from the first entry and column 4 would be the key of the other one:
use warnings;
use strict;
my $seq;
while(<>){
chomp;
my #line = split;
$seq->{"$line[2]" } = [#line];
}
foreach my $s (keys %{$seq} ) {
### #{ $seq->{$s}}[3] key for the second pair
print #{ $seq->{$s}}[0],"\t",#{ $seq->{$s}}[1],"\t",#{ $seq->{$s}}[2],"\t",#{ $seq->{$s}}[3],"\t",#{ $seq->{#{ $seq->{$s}}[3]}}[0],"\t",#{ $seq->{#{ $seq->{$s}}[3]}}[1],"\t",#{ $seq->{#{ $seq->{$s}}[3]}}[2],"\t",#{ $seq->{#{ $seq->{$s}}[3]}}[3],"\n";
delete ${$seq}{#{ $seq->{$s}}[3]};
}
it works but there is a small problem!
I do no know how to delete the second key (in this case 3009848 in the second entry) without getting the following error:
NS501570 WWW 3009824 3009848 NS501572 WAD 3009848 3009898
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in hash element at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in print at find_mate2.pl line 22, <> line 2.
Use of uninitialized value in delete at find_mate2.pl line 23, <> line 2.
It looks like you are deleting 3009848 before the 3009848 key is iterated in the foreach loop. You can probably try a simple next unless defined $seq->{$s}; since you've deleted $s==3009848 on the interation for 3009824. defined checking never hurts...
The warnings are due to the fact that you are deleting elements of your hash while you are iterating over them, which is in general, a bad idea.
Perl is warning you that you are trying to print something that is no longer there. Also, be aware that the keys are returned in random order unless you sort them.
Take a look at:
How should I delete hash elements while iterating?

How does perl recognise end of variable while printing?

How does perl recognise end of variable?
For example, this code:
use warnings;
my $a = 10;
print "Value of a is $a:::";
Output:
Use of uninitialized value $a:: in concatenation (.) or string at tryprint.pl line 6.
Value of a is :
Why does it consider $a:: and not $a: or $a:::
This works:
print "Value of a is $a\:::";
prints:
Value of a is 10:::
:: is used to print/access a variable from a package/symbol table. For eg, to access a scalar variable x in package abc, Perl uses $abc::x where abc is the name of the symbol table and x is the variable. Similarly, when you used $a:::, Perl thought there is a package whose name is 'a' and the variable name as :, and hence those error.
See this example below:
our $a = 10;
{
my $a=20;
print "lexical a is $a \n";
print "Value of a is $main::a";
}

Perl syntax error: Bareword found where operator expected

As the title suggests how could I accomplish this?
I have been following a tutorial, but I get a syntax error:
Bareword found where operator expected at arrays_and_variables.pl line
26, near "$2names"
(Missing operator before names?) syntax error at
arrays_and_variables.pl line 26, near "$2names " Execution of
arrays_and_variables.pl aborted due to compilation errors.
The code I have so far is:
#names = ('james','dylan','max');
# join elements of array into a schalar variable.
$2names = join ('', #names);
print $s2names;
2names is an invalid variable name. Names can't start with a number—they have to begin with a letter or an underscore.

Infinite loop in perl Carp module

We have some code which catches an exception, logs the message and then calls Carp::longmess to get the stacktrace.
So a simplified view of what we are doing is:
eval { <some SOAP::Lite stuff> };
if( my $err = $# )
{
logwrite( "Caught Error: $err" );
}
The logwrite function is essentially:
sub logwrite($)
{
my $msg = $_[0];
my($pkg,$fil,$lin)=caller;
my $timestamp = POSIX::strftime(...);
print STDERR "$timestamp $fil/$lin $msg\n";
print STDERR "$timestamp $fil/$lin Stack trace:\n" . Carp::longmess . "\n";
}
But in the log I am seeing:
20111030 Module.pm/42 Caught Error: at line
Use of uninitialized value in caller at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 22.
Use of uninitialized value in string eq at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 91.
Use of uninitialized value in numeric lt (<) at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 200.
Use of uninitialized value in pattern match (m//) at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 55.
Use of uninitialized value in concatenation (.) or string at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 55.
Use of uninitialized value in concatenation (.) or string at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 142.
Use of uninitialized value in concatenation (.) or string at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 142.
Use of uninitialized value in concatenation (.) or string at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 142.
Use of uninitialized value in concatenation (.) or string at /usr/lib/perl5/5.8.8/Carp/Heavy.pm line 142.
...
And that sequence of warnings from the Carp/Heavy.pm module repeats over and over again indefiniately, blowing out the logifle. So we eventually kill it off. These warnings look like they're being triggered by the call to Carp::longmess. The other intersting thing here is the $# variable appears to just be at. It as the at added by die, but no actual error message or line number.
Has anyone seen this before or have any idea what's coing on with the Carp package? This is rare, but has happenned a handful of times over the past month or so, and we have hundreds of these jobs running every day.
Your code works for me on perl v5.10.1, with Carp.pm version 1.11.
However, note that what it does is perhaps not what you expect: the backtrace produced by longmess will show where the logwrite function was called from, not where the actual error occurred inside the eval.
I realize this doesn't answer your actual question, but . . . since apparently $msg eq 'at line ' in this case, maybe you should just bypass the issue by tacking unless $msg eq 'at line ' onto the end of the print ... Carp::longmess ... statement? (I mean, unless someone proposes a real solution.)