Is this a Hash and how can I test a value?

J

Jockser

Hello I have a XML doc that I'm processing. I'm down to the point where in
the for loop I need to check to see if 'type' is set to 'a' or 'b'.
Using print Dumper( $temp ) gives me this:

$VAR1 = {
'lname' => 'Smith',
'fname' => 'John',
'address1' => '2500 Lemon St.',
'type' => 'a'
};

1st question: Is this a Hash? I haven't been sucessful in accessing it
like a hash, ofcourse this is the first time I've delt with Hashes.

2nd question: How can I check to see if 'type' is = to 'a' or 'b' ?

Code here:

use strict;
use warnings;
use Template;
use XML::Simple;
use Data::Dumper;


my $cust_xml = XMLin('./test2.xml'); #, ForceArray => 1);
my $input = 'temp_a.tt';
my $tt = Template->new( );

for my $temp (@{$cust_xml->{template}}) {

# Need some code here to test the value of 'type', if it is 'a' or 'b'
# I can then do an if statement to change the $input file
# $input = 'temp_b.tt';

$tt->process($input, $temp)
|| die $tt->error( );
print Dumper( $temp );
}
print "\n";
#print XMLout($cust_xml);
print "\n";



Thanks!
 
B

Brian Wakem

Jockser said:
Hello I have a XML doc that I'm processing. I'm down to the point where
in the for loop I need to check to see if 'type' is set to 'a' or 'b'.
Using print Dumper( $temp ) gives me this:

$VAR1 = {
'lname' => 'Smith',
'fname' => 'John',
'address1' => '2500 Lemon St.',
'type' => 'a'
};

1st question: Is this a Hash? I haven't been sucessful in accessing it
like a hash, ofcourse this is the first time I've delt with Hashes.


$temp contains a reference to a hash.

2nd question: How can I check to see if 'type' is = to 'a' or 'b' ?


print $temp->{'type'};
 
T

Tad McClellan

Jockser said:
$VAR1 = {
'lname' => 'Smith',
'fname' => 'John',
'address1' => '2500 Lemon St.',
'type' => 'a'
};

1st question: Is this a Hash?


No.

$temp is a reference (to a hash).

See:

perldoc perlreftut

2nd question: How can I check to see if 'type' is = to 'a' or 'b' ?


if ( $temp->{type} eq 'a' or $temp->{type} eq 'b' ) {
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top