Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

XML::Simple and arrays

by J9 (Beadle)
on Mar 07, 2003 at 14:38 UTC ( [id://241144]=perlquestion: print w/replies, xml ) Need Help??

J9 has asked for the wisdom of the Perl Monks concerning the following question:

Appologies if this turns out to be a long post.

I have the following xml file:..
<?xml version="1.0"?> <!-- This is XML for the Notify Mapping --> <map> <CI name="ABC"> <recipient id="Usr-One"> <alert method="SMS" dest="+44123456789" st="" et=""> <enable>Y</enable> </alert> <alert method="EMAIL" dest="usr-one@nowhere.co.uk" st="" e +t=""> <enable>Y</enable> </alert> </recipient> <recipient id="User-two"> <alert method="SMS" dest="+44987654321" st="" et=""> <enable>Y</enable> </alert> <alert method="EMAIL" dest="user-two@anywhere.co.uk" st="" + et=""> <enable>Y</enable> </alert> </recipient> </CI> <CI name="DEF"> <recipient id="Usr-One"> <alert method="SMS" dest="+44123456789" st="" et=""> <enable>Y</enable> </alert> </recipient> </CI> </map>
Basically what I want to do is, if I have a var that matches the CI name I want to (at this point) print out EACH alert method and dest. (I am not doing anything with either st or et yet.)

So this is what I have so far ... (but I cant get any further)
use strict; use warnings; use XML::Simple; #use Data::Dumper; # I put this here to see what the hash looks like my $check = "ABC"; my $alert_config_file = "C:/Path to XML/AlertCIMap.xml"; my $XMLSimple = XML::Simple->new(forcearray => 1, keyattr => {}); eval { $rules = $XMLSimple->XMLin("$alert_config_file")}; if ($@) { print "Problems Reading $alert_config_file: $@\n"; exit; } #print Dumper($rules); if (&CI_Exists($check)) { print "Matched\n"; } else { print "NOT FOUND\n"; } ## SUB ROUTINES ## sub CI_Exists #Check if CI exists in Mapping { my $chk = shift; my $rc = 0; #Foreach name in xml look for match my $ci = $rules->{CI}; foreach my $n(@$ci) { if ($n->{name} eq $chk){ print "Chk = $chk\n"; #Just for testing so I can s +ee stuff is happening #THIS NEXT LINE IS CAUSING ME FRUSTRATION... my $rec_list = $n->{name}->{recipients}; #List of +Recepients foreach my $recipient(@$rec_list) { #foreach Alert create file. print "Recipient = $recipient\n"; my $alert_list = # What should go here +..?? List of Alerts foreach my $alert($alert_list){ print"I Would Send a $alert to $recipient\n"; } } $rc = 1; last; } } return($rc); }
How can I check each recipient and, foreach recipient check each alert method..?
Is there a better way to layout the xml file as I have control over this?

Many Many thanks,
J9

Replies are listed 'Best First'.
Re: XML::Simple and arrays
by broquaint (Abbot) on Mar 07, 2003 at 14:59 UTC
    sub CI_Exists { my $chk = shift; my @nodes = map $rules->{CI}->{$_}, grep { $_ eq $chk } keys %{ $rules->{CI} }; foreach my $n (@nodes) { print "Chk = $chk\n"; foreach my $rec (keys %{ $n->{recipient} }) { foreach my $alert (@{$n->{recipient}->{$rec}->{alert}}) { ## or whatever suits alert_recipient(@$alert{qw/method dest/}, $rec) } } return 1; } return 0; }
    Now just fill in sub alert_recipient or whatever works for you and it should do as you wish.
    HTH

    _________
    broquaint

    update (broquaint): code now does the intended thing
    update 2 (broquaint): fixed keys %$rules->{CI} (that'll teach me for posting untested code)

      Thank you,

      However when I try the above code I get this:
      Type of arg 1 to keys must be hash (not hash element)
      What does that mean?

      Oh and it's on the grep { $_ eq $chk } keys %$rules->{CI}; line

      Thanks for the help.

        That should be grep { $_ eq $chk } keys %{ $rules->{CI} };, because without the braces, the % deferences the $rules hashref and not the $rules->{CI} hashref.

        See References quick reference for more illumination. HTH!

        If not P, what? Q maybe?
        "Sidney Morgenbesser"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://241144]
Approved by robartes
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found