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

Hello monks,
I have the following XML file structure:
<attr-name>model_name</attr-name> <alternate> <mtype>0x234567</mtype> <attr-id>0x654321</attr-id> </alternate> <default-id>0x654321</default-id> <attr-name>security_string</attr-name> <alternate> <mtype></mtype> <attr-id></attr-id> </alternate> <default-id>0x123456</default-id>
What I need to do is have the default-id point to the attr-name for a mtype I pass in unless a mtype is specified, and in that case I need to have that mtype point to the attr-id specified. (i.e)

for security_string I pass in mtype 0x23456 so it uses the default-id of 0x10009 but in the case of model name it should use the attr-id of 0x654321.

Any suggestions for a data structure or alogorithm to use?

Replies are listed 'Best First'.
Re: Data structure advice needed
by CombatSquirrel (Hermit) on Nov 18, 2003 at 13:27 UTC
    I don't quite get what you want, I do especially not know where you get that default-id of 0x10009 from, but this might (or might not) get you started:
    #!perl -w use strict; use Data::Dumper; my @fields = qw/attr-name mtype attr-id default-id/; chomp(my @data = <DATA>); my (@parsed_data, %temp_hash, %data_hash); while (@data >= @fields) { @temp_hash{@fields} = splice @data, 0, @fields; push @parsed_data, { %temp_hash }; } for (@parsed_data) { if ($_->{mtype}) { $data_hash{$_->{'mtype'}} = { VALUE => $_->{'attr-id'}, TYPE => 'attr-id' }; } else { $data_hash{$_->{'default-id'}} = { VALUE => $_->{'attr-name'}, TYPE => 'attr-name' }; } } print Dumper \%data_hash; __DATA__ model_name 0x234567 0x654321 0x654321 security_string 0x123456

    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: Data structure advice needed
by eric256 (Parson) on Nov 18, 2003 at 14:42 UTC

    I think it might be worth pointing out that the XML you provided is not realy a good layout. Perhaps you get it from somwhere else but I would consider it a main problem.

    Since the order of elements in XML should not be a factor in there display, picture that code with the lines out of order and you'll see there is no relation (XML wise) between some of your attributes. This does not mean of course that you can't use that layout, just remember that it should be considered a modified subset of XML and that some Parsers may parse it correctly while others may not.

    <default-id>0x654321</default-id> <default-id>0x123456</default-id> <attr-name>security_string</attr-name> <attr-name>model_name</attr-name> <alternate> <mtype>0x234567</mtype> <attr-id>0x654321</attr-id> </alternate> <alternate> <mtype></mtype> <attr-id></attr-id> </alternate>

    You can see all the same data is there but now its lacking meaning, and that parses as the same XML as above. A solution might be to wrap each element inside another tag.

    <attr name="model_name"> <alternate> <mtype>0x234567</mtype> <attr-id>0x654321</attr-id> </alternate> <default-id>0x654321</default-id> </attr> <attr name="security_string"> <alternate> <mtype></mtype> <attr-id></attr-id> </alternate> <default-id>0x123456</default-id> </attr>

    ___________
    Eric Hodges
Re: Data structure advice needed
by Art_XIV (Hermit) on Nov 18, 2003 at 13:55 UTC

    Could you re-explain the problem? Your situation is terribly unclear.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"