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

Discussion in 'Perl Misc' started by Jockser, Jun 28, 2006.

  1. Jockser

    Jockser Guest

    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!
     
    Jockser, Jun 28, 2006
    #1
    1. Advertising

  2. Jockser

    Brian Wakem Guest

    Jockser wrote:

    > 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'};



    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Jun 28, 2006
    #2
    1. Advertising

  3. Jockser <> wrote:

    > $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' ) {


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 28, 2006
    #3
  4. Jockser

    Jockser Guest

    Thanks for the responses worked out good.
     
    Jockser, Jun 29, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Allan Schwartz
    Replies:
    7
    Views:
    373
    Marco Manfredini
    Aug 13, 2004
  2. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    704
    Skybuck Flying
    Oct 4, 2009
  3. rp
    Replies:
    1
    Views:
    532
    red floyd
    Nov 10, 2011
  4. Une bévue
    Replies:
    5
    Views:
    153
    Une bévue
    Aug 10, 2006
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    624
    David A. Black
    Jul 2, 2008
Loading...

Share This Page