in reply to Re^13: the annoying keys "key" and "value"
in thread the annoying keys "key" and "value"

So you want to have an ordered associative array by using: [{'key'=>somekey1,'value'=>somevalue1},...] because that is superior to having: $order=[somekey1,somekey2,...]; and {somekey1=>somevalue1,...} ?

Is this because using the associative array for its primary function -- looking up values from keys -- is so much more convenient and efficient your way?

Replies are listed 'Best First'.
Re^15: the annoying keys "key" and "value"
by ikegami (Patriarch) on Dec 24, 2010 at 16:46 UTC

    It's not a question of efficiency, it's a question of actually working. $order=[somekey1,somekey2,...]; and {somekey1=>somevalue1,...} doesn't support more than one value with the same key.

      For clarity, what you are calling an ordered associative array is actually an ordered relation. The definition of "associative array" is functional (ie: one value per key), not relational (ie: potentially N values per key). So we have:
      <ordered_relation_foo> <b>2</b> <b>4</b> <a>1</a> <b>6</b> <a>3</a> <a>5</a> </ordered_relation_foo>
      becomes:
      $ordered_relation_foo={ _meta_order=>[b,b,a,b,a,a], b=>[2,4,6], a=>[1,3,5]};
      So now a query of this ordered relation becomes:
      $ordered_relation_foo{a}
      How does that compare to the query of your structure?

        what you are calling an ordered associative array is actually an ordered relation.

        Perhaps. The choice of name doesn't change the point, though.

        How does that compare to the query of your structure?

        It contains the same information, and it does appear to be faster for some operations (comparable for the rest).

        > The definition of "associative array" is functional (ie: one value per key), not relational (ie: potentially N values per key).

        Whose definition? Not wikipedias!

        Language implementations differ, and users of different languages have problems to find reliable common terminology.

        Many monks already showed you that XML and HTML generally allow repeated tags with different entries.

        If you don't like this, please find an XML forum to complain, it has nothing to do with Perl!

        Or give me a reply to Re^3: the annoying keys "key" and "value" (XML-RPC) showing that multi valued or ordered keys are excluded by RPC's scheme definition.

        Cheers Rolf

        I went ahead and implemented an HTML parser to produce a "DOM" in accordance with the aforementioned structure.

        Here is the program to produce HTML from that "DOM":

        sub htmlize{ my ($tag_name,$dom)=@_; return $dom unless ref $dom; my $html; my %cnt; my $no_text; $html .="<$tag_name"; # print "<$tag_name"; for my $rank (0..$dom->{'_meta_attr_n'}-1){ my $attr_name=$dom->{'_meta_order'}->[$rank]; if($attr_name eq '/'){ $no_text=1; next; } my $attr=$dom->{$attr_name}->[$cnt{$attr_name}||'0']; $attr = join ', ',@$attr if ref $attr; $html .= " $attr_name=\"$attr\""; # print " $attr_name=\"$attr\""; $cnt{$attr_name}++; # in the unlikely event that a child tag a +nd this attr have the same name } $html .= '/' if $no_text; # print '/' if $no_text; $html .= '>'; # print '>'; for my $rank ($dom->{'_meta_attr_n'}..$#{$dom->{'_meta_order'}}){ my $tag_name=$dom->{'_meta_order'}->[$rank]; $html .= htmlize($tag_name,$dom->{$tag_name}->[$cnt{$tag_name} +||'0']); $cnt{$tag_name}++; } $html.="</$tag_name>" if !$no_text; # print "</$tag_name>" if !$no_text; return $html; }

        Is this unwieldy compared to the [{'key'=>$key_name, 'value'=>$value},...] structure?