in reply to Manipulating data structure

This puzzles me slightly, because it appears to just be a POST data-stream which any of several CGI-handling packages already know how to handle.   But, nevertheless, the so-called “auto-vivification” features of Perl make such things much easier than what you seem to be doing here.   Consider the following one-liner:

perl -e 'my $foo; push @{$$foo{'bar'}}, 3; use Data::Dumper; print Data::Dumper->Dump([\$foo],["foo"]);' $foo = \{ 'bar' => [ 3 ] };

As you can see:

Perl has, in short, followed its usual practice of “do what I mean.”

You could, therefore, simply loop through the set of keyword=value pairs, and push a value into a bucket for that keyword, trusting auto-vivification to automagically make those elements properly for you.   (Perhaps you also use:   next unless defined($value); ... in that loop.)   Welcome to “The Swiss Army® Knife of Data Processing.”

If duplicates are a possibility and considered to be a problem, auto-vivification can be used just as easily to make a hash of hashes, e.g.
$$foo{$keyword}{$value} = 1;
or something similar.   In any case, Perl allows you to achieve the desired results with a paucity of code.

Replies are listed 'Best First'.
Re^2: Manipulating data structure
by SerZKO (Beadle) on Aug 16, 2012 at 18:06 UTC
    Hej Sundialsvc4 and thanks for your reply,

    I'm not really sure if I understood everything you wrote, but I think I might understood your idea.

    But how to assign for example key CMG12 to 12th UID in the file ?

      As you parse the file one line at a time and come to CMG1=Ja, the string CMG1 is your keyword and Ja is the associated value.   If you needed to know that it was in the twelfth position, then you could for example set up a hash-of-hashes data structure such that $hoh->{'CMG1'}{'Ja'} = 12.   Or whatever else suits your fancy.   My key point was that, with auto-vivification, you can establish such data structures very easily. You refer to an unassigned variable as a hash ... and it is a hash.   You execute as an assignment-statement what I wrote just above, and the desired hash-of-hash element simply appears.   $hoh->{'CMG1'} exists, and it has a key {'Ja'}, and that key has the value 12.   Presto.   Arrays work the same way.   It’s entirely up to you to determine what structure you want, but very easy to get it.

        Hej Sundialsvc4 and thanks for your post,

        I've done some changes in this subroutine so it looks like this now:

        my @entries; my $previouskey = ""; open (KOF, "<$konfil") or die "Kan inte \xF6ppna filen $konfil"; open (NYF, ">$nyfil") or die "Kan inte \xF6ppna filen $nyfil"; flock (NYF,2) or die $!; my $offset = 0; while(<KOF>) { chomp; $value =~ s/^\s*(\S*)\s+$/$1/; if ($key =~ /^(.*)(\d+)$/) { ##### #### #### $key = $1; # # # # # # $entries[$2]{$key} = $value; ##### # # # # next; # # # # # # } # # #### #### unless ($key eq $previouskey || $previouskey eq "") { $offset = 0; } $previouskey = $key; $entries[$offset]{$key} = $value; $offset++; } my @sorted = sort { $a->{'NYTT'} <=> $b->{'NYTT'} } @entries; my $pers =0; foreach my $e (@sorted) { print NYF ($e->{'UID'}, "|", $e->{'FNA'}, "|", $e->{'ENA'}, "|", $ +e->{'PRE'}, "|", $e->{'NYTT'}, "|", $e->{"PRI"}, "|", $e->{"FOR"}, "| +", $e->{"CMG"}, "|", $e->{"MEX"}, "\n") if( $e->{"UID"} ne "" || $e-> +{"FNA"} ne "" || $e->{"ENA"} ne ""); $pers++ if ($e->{"UID"} ne ""); } close KOF; close NYF;
        and it seems to work !

        Thanks for pointing me to the right direction