test value

S

Shane

I have some nasty code, that I want to check the value of something held in
a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []

How do I test for that?
 
S

Sisyphus

Shane said:
I have some nasty code, that I want to check the value of something held in
a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []

How do I test for that?

Maybe the Devel::peek module's Dump($val) will give you what you want.
Certainly worth a try.

(That's only a guess, of course ..... with the information you've provided a
guess is about all you'll get :)

Cheers,
Rob
 
J

John Bokma

Shane said:
I have some nasty code, that I want to check the value of something
held in a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []

$val contains a reference to an empty array, like:

my @array = ();
my $val = \@array;
 
J

Josef Moellers

John said:
I have some nasty code, that I want to check the value of something
held in a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []


$val contains a reference to an empty array, like:

my @array = ();
my $val = \@array;

if (@$val == 0) {
print "Empty\n";
}
 
J

John Bokma

Josef Moellers said:
John said:
I have some nasty code, that I want to check the value of something
held in a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []


$val contains a reference to an empty array, like:

my @array = ();
my $val = \@array;

if (@$val == 0) {
print "Empty\n";
}

@$val or print "Empty\n";
 
N

Nick of course

Shane said:
I have some nasty code, that I want to check the value of something held in
a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []

How do I test for that?

if (ref $val eq 'ARRAY' && @$val == 0) { ... }
 
S

Shane

John said:
Josef Moellers said:
John said:
I have some nasty code, that I want to check the value of something
held in a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []


$val contains a reference to an empty array, like:

my @array = ();
my $val = \@array;

if (@$val == 0) {
print "Empty\n";
}

@$val or print "Empty\n";


Apologies for the delay between your answer and my reading the post

To make matters slightly more complicated $val is an arrayref, in a hash
If I use the following if @($hash->{$val}) I get
Scalar found where operator expected at ./attempt3.pl line 254,
near "@($hash"
(Missing operator before $hash?)
Scalar found where operator expected at ./attempt3.pl line 255, near "$val"
(Missing semicolon on previous line?)
syntax error at ./attempt3.pl line 254, near "@($hash"
syntax error at ./attempt3.pl line 260, near "}"
Execution of ./attempt3.pl aborted due to compilation errors.
 
S

Shane

Nick said:
I have some nasty code, that I want to check the value of something held
in a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []

How do I test for that?

if (ref $val eq 'ARRAY' && @$val == 0) { ... }


Unfortunately this expression captures other references that have a value in
the array they point to
 
C

Ch Lamprecht

Shane said:
To make matters slightly more complicated $val is an arrayref, in a hash
If I use the following if @($hash->{$val}) I get
Scalar found where operator expected at ./attempt3.pl line 254,

One way of dereferencing arrayrefs is:

@{$arrayref}

However, you are using $val as a hash key here...
You didn't tell anything about the value of $hash->{$val}.
It would be much easier to help you, if you posted a short but complete example.


Documentation on perl references and data structures:
perldoc perlreftut
perldoc perldsc
perldoc perllol

Christoph
 
S

Shane

Ch said:
One way of dereferencing arrayrefs is:

@{$arrayref}

However, you are using $val as a hash key here...
You didn't tell anything about the value of $hash->{$val}.
It would be much easier to help you, if you posted a short but complete
example.


Documentation on perl references and data structures:
perldoc perlreftut
perldoc perldsc
perldoc perllol

Christoph

Yes I should have posted the full problem from the beginning
The code is spread over 300+ lines, modular, but still not practical to
post

As I started this reply I had a thought and checked my code, I realised I
was reading the output incorrectly, as I had thought my capture was
reporting false positives
However, I realised I needed the output to show which element it was talking
about, and sure enough it was talking about the 'broken' element multiple
times (The capture was working fine IOW)

Thanks all for your time and fixes
 
J

John Bokma

Shane said:
Yes I should have posted the full problem from the beginning
The code is spread over 300+ lines, modular, but still not practical
to post

Exactly, and common advice is to reduce such problems to just the few
lines that cause the problem. Very often people are able to find the error
themselves that way and save "us" a lot of time :)

Moreover, 300+ lines sound a lot for something you call modular :)
 
J

Josef Moellers

Shane said:
John Bokma wrote:

John Bokma wrote:




I have some nasty code, that I want to check the value of something
held in a scalar, but there is 'no' value held in there

Data::Dumper tells me the $val = []


$val contains a reference to an empty array, like:

my @array = ();
my $val = \@array;

if (@$val == 0) {
print "Empty\n";
}

@$val or print "Empty\n";



Apologies for the delay between your answer and my reading the post

To make matters slightly more complicated $val is an arrayref, in a hash
If I use the following if @($hash->{$val}) I get

Shouldn't that be @{$hash->{$val}}
(curlies iso parentheses)?

Josef
 

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
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top