in reply to XML::Simple and default sorting...

After looking at the docs, I think you're shooting yourself in the foot by using keyattr. It's converting what would have been an array into a hash. Since elements of a hash are not ordered, using keyattr is causing you to lose the order of your XML records.

Replies are listed 'Best First'.
Re^2: XML::Simple and default sorting...
by amonotod (Acolyte) on Aug 10, 2004 at 22:07 UTC
    Looks as though you are correct. I took out the zsql:field part of the XML parsing, and am now getting the fields in order. Just need to fix the SQL statement building now, and I'll be good to go. Will post the final code tomorrow.

    Thanks! I appreciate the time you took to look at this...
    amonotod

Re^2: XML::Simple and default sorting...
by amonotod (Acolyte) on Aug 11, 2004 at 15:03 UTC
    I wanted to take a moment to say thanks, so, "Thanks!". I appreciate all the help that is given on this site...

    #!perl -w use strict; no utf8; use Encode; use Unicode::String; use XML::Simple; my %primKeys; my $schema = XMLin("./TEST_Baseline_0.99.xml", forcearray => [ qw(zsql:database zsql:table zsql:col +umn zsql:index) ], keyattr => { "zsql:database" => 'name', "zsql:table" => 'name', "zsql:column" => 'name', "zsql:index" => 'name'} ); foreach my $db_table (sort keys %{$schema->{"zsql:table"}} ) { if ($db_table ne '') { #print "\t\t$table\n"; my $create_statement = "create table $db_table ("; my $primarykey= ", PRIMARY KEY ("; my $counter = 1; my $countto = keys %{$schema->{"zsql:table"}->{$db_table}->{'zsql: +column'}}; #print "Counter = $counter, count-to = $countto\n"; foreach my $tableColumn (sort keys %{$schema->{"zsql:table"}->{$db +_table}->{'zsql:column'}} ) { my $col_name = remCol($tableColumn); my $col_type = $schema->{'zsql:table'}->{$db_table}->{'zsql:colu +mn'}->{$tableColumn}->{type}; $create_statement .= "$col_name $col_type"; if (defined $schema->{"zsql:table"}->{$db_table}->{'zsql:column' +}->{$tableColumn}->{notnull} ) { $create_statement .= " NOT NULL"; } else { $create_statement .= " NULL"; } $counter++; if ( $counter <= $countto ) { $create_statement .= ", "; } if (defined $schema->{"zsql:table"}->{$db_table}->{'zsql:column' +}->{$tableColumn}->{primarykey} ) { push (@{$primKeys{$db_table}{key}}, $col_name); if (length($primarykey) > 15) { $primarykey .= ", $col_name"; } else { $primarykey .= "$col_name"; } } } if (length($primarykey) > 15) { $primarykey .= " )"; $create_statement .= $primarykey; } $create_statement .= ")"; print "Creating table $db_table with statement\n\t$create_statemen +t\n\n"; foreach my $db_index (sort keys %{$schema->{'zsql:table'}{$db_tabl +e}->{'zsql:index'}} ){ if (exists $schema->{"zsql:table"}->{$db_table}->{'zsql:index'}- +>{$db_index}->{'zsql:field'}) { #print $schema->{"zsql:table"}->{$db_table}->{'zsql:index'}->{ +$db_index}->{'zsql:field'} ."\n"; my @thisIndex; my $fld_add = 1; my $sqlStatemnt = "CREATE INDEX $db_index on $db_table ("; my (@columns, $fld_cnt); if (ref($schema->{"zsql:table"}->{$db_table}->{'zsql:index'}-> +{$db_index}->{'zsql:field'}) eq "ARRAY") { foreach my $field (@{$schema->{"zsql:table"}->{$db_table}->{ +'zsql:index'}->{$db_index}->{'zsql:field'}}) { foreach (keys(%{$field}) ) { push @columns, ${$field}{$_}; + } } } elsif (ref($schema->{"zsql:table"}->{$db_table}->{'zsql:inde +x'}->{$db_index}->{'zsql:field'}) eq "HASH") { foreach (keys(%{$schema->{"zsql:table"}->{$db_table}->{'zsql +:index'}->{$db_index}->{'zsql:field'}})) { push @columns, ${$schema->{'zsql:table'}->{$db_table}->{'z +sql:index'}->{$db_index}->{'zsql:field'}}{$_}; } } $fld_cnt = scalar(@columns); if ($fld_cnt) { foreach my $index_col (@columns ) { my $col_name = remCol($index_col); push (@thisIndex, $col_name); $sqlStatemnt .= "$col_name"; if ($fld_add < $fld_cnt) { $sqlStatemnt .= ", "; } else { $sqlStatemnt .= ") "; } $fld_add++; } if (@thisIndex ne @{$primKeys{$db_table}{key}}) { print "Adding index $db_index to table $db_table with \n"; print "\t$sqlStatemnt\n\n"; } } } } } } sub remCol { my ($retval) = @_; $retval =~ s/Col\d+\-\-//; return $retval; }
    Again, thanks,
    amonotod