in reply to (jeffa) Re: XML::Simple
in thread How to access results of XML::Simple?

jeffa's (and PodMaster's) replies are correct, of course... but I want to stress that besides there being no 'gene' key, you can't do this:   print $xml->{'gene' => {'id', 'label'}}, "\n"; That will never work. It is syntactically bogus. Why did you think you could do that?
You could do this:   print "$xml->{'gene'}{'id'}, $xml->{'gene'}{'label'}\n"; or even   print @{ $xml->{'gene'} }{'id','label'}, "\n"; assuming the data structure matches that.

jdporter
...porque es dificil estar guapo y blanco.

Replies are listed 'Best First'.
Re: Re: (jeffa) Re: XML::Simple
by PodMaster (Abbot) on Dec 18, 2002 at 01:20 UTC
    What?!?

    It is syntactically valid, it just won't do what he wants (no chance in hell).

    The expression will evaluate to $xml->{{}}, and that key (the stringified version of that anonymous hashref) is not defined, so a warning will be thrown.
    something like that ;)(actually $xml->{gene}{{}} , but it's not really important)

    If you didn't know what the comma operator would do in this expression, read "List" is a Four-Letter Word, as well as perldoc perlop

    update:

    use Data::Dumper; local $\="\n"; $a = { gene => { id => 3, label => 'generrrous' } }; print $a->{ gene, id}=1; print $a->{ gene => id }; print Dumper $a; __END__ 1 1 $VAR1 = { 'gene' => { 'label' => 'generrrous', 'id' => 3 }, 'gene‡˜id' => 1 };


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      If I want to access something nested within the gene tags. For example, <gene id = "1"><gene_seq id = "1"></gene_seq></gene>. How do I do that?

      print $xml->{gene_seq}{$id}{'startpos'}, "\n";
      Does not do it.
        #!/usr/bin/perl -w use strict; use warnings; use XML::Simple; use Data::Dumper; my $data = do {local $/;<DATA>}; my $xml = new XML::Simple(keeproot => 2);# this is what you want $xml = XMLin($data); print Dumper($xml), "\n\n"; for my $id (keys %{ $xml->{gene} }) { # print $xml->{gene}{$id}{'label'}, "\n"; # print $xml->{gene_seq}{$id}{'label'}, "\n"; print $xml->{gene}{$id}{gene_seq}{'startpos'}, "\n"; #print "$xml->{'gene'}{'id'}, $xml->{'gene'}{'label'}\n"; } __DATA__ <?xml version="1.0" ?> <many_genes> <gene id = "1" label = "gene_of_interest"> <gene_seq id = "1" startpos = "5999"/> </gene> <gene id = "2" label = "Another_gene_of_interest"> <gene_seq id = "2" startpos = "96819"/> </gene> </many_genes>
        yields
        $VAR1 = {
                  'gene' => {
                              '1' => {
                                       'gene_seq' => {
                                                       'startpos' => '5999',
                                                       'id' => '1'
                                                     },
                                       'label' => 'gene_of_interest'
                                     },
                              '2' => {
                                       'gene_seq' => {
                                                       'startpos' => '96819',
                                                       'id' => '2'
                                                     },
                                       'label' => 'Another_gene_of_interest'
                                     }
                            }
                };
        
        
        5999
        96819
        It's like a tree ;)
        # you start with
        $xml->{gene}->{1}->{gene_seq}->{startpos};
        # and you make it variable
        $xml->{gene}->{$id}->{gene_seq}->{startpos};
        # and its same as
        $xml->{gene}{$id}{gene_seq}{startpos};
        $$xml{gene}{$id}{gene_seq}{startpos};
        $$xml{gene}->{$id}{gene_seq}->{startpos};
        


        MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
        ** The Third rule of perl club is a statement of fact: pod is sexy.