Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: Re: Using hash elements to create a string

by RollyGuy (Chaplain)
on Sep 10, 2002 at 14:53 UTC ( #196695=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Using hash elements to create a string
in thread Using hash elements to create a string

I see where I missed part of your specification. Here is try number 2. Now, the thing to note here is that my output is slightly different than the one you posted above. The "whatever = something" pairs are moved around a little bit. If I remember my SQL syntax, I don't believe it should matter too much. Let me know if it does, and I can help you fix it. Anyhow, here is the new updated code:

%hash = ( "view0" => [qw/buffersize size value speed/], "view1" => [qw/buffersize size value speed/], "view2" => [qw/buffersize size value speed/], "view3" => [qw/buffersize size value speed/]); my $lastview; my $output; foreach $key (sort(keys %hash)){ # Do this only the first time. if(not defined($lastview)){ $lastview = $key; $output = "$key"; next; } $output .= " full outer join $key on "; my $i; $array_elements_used = scalar(@{ $hash{$key} })-1; #loop through all of the elements except for the last for($i=0;$i<$array_elements_used;$i++){ $output .= "$lastview.$hash{$lastview}[$i] = $key.$hash{$key}[$i]" +; # only add an "and" if this is not the last element used. $output .= " and " if ($i != ($array_elements_used-1)); } $lastview = $key; } print $output, "\n"; ============= output: view0 full outer join view1 on view0.buffersize = view1.buffersize and + view0.size = view1.size and view0.value = view1.value full outer joi +n view2 on view1.buffersize = view2.buffersize and view1.size = view2 +.size and view1.value = view2.value full outer join view3 on view2.bu +ffersize = view3.buffersize and view2.size = view3.size and view2.val +ue = view3.value

Replies are listed 'Best First'.
Re:x4 Using hash elements to create a string
by johnirl (Monk) on Sep 11, 2002 at 08:30 UTC
    Hey RollyGuy
    thanks again for all your help. Your code works perfectly and I nearly have it integrated into ma program. But could you (or anybody else) tell me whats wrong with the code below.?
    Thanks
    #!/usr/bin/perl use strict; my @views = ( 'view0', 'view1'); my $num_of_views = scalar(@views); my %hash; my @global_include = ("size, buffersize, value", "size, buffersize, va +lue"); my $lastview; my $output; for (my $j = 0; $j < $num_of_views; $j++){ my @array = split (", ",$global_include[$j]); pop @array; $hash{$j} = \@array; } #for (my $j = 0; $j < $num_of_views; $j++){ # my $ref = $hash{$j}; # print "HASH CONTAINS FOR $j: "; # foreach (@$ref){ # print "$_\n"; # } #} foreach my $key (sort(keys %hash)){ # Do this only the first time. if(not defined($lastview)){ $lastview = $key; $output = "$key"; next; } $output .= " full outer join $key on "; my $i; my $array_elements_used = scalar(@{ $hash{$key} })-1; #loop through all of the elements except for the last for($i=0;$i<$array_elements_used;$i++){ $output .= "$lastview.$hash{$lastview}[$i] = $key.$hash{$key}[$i]" +; # only add an "and" if this is not the last element used. $output .= " and " if ($i != ($array_elements_used-1)); } $lastview = $key; } print $output, "\n";

    j o h n i r l .

    Sum day soon I'Il lern how 2 spelI (nad tYpe)

      In your for loop that creates the hash, you are using just $j as the key to the hash. That will result in the keys just being numbers. You actually want the keys to be the values in the array @views that correspond to the entry $j. So you will need to change:
      $hash{$j} = \@array;
      into
      $hash{$views[$j]} = \@array;

      As an additional note, the perl debugger will easily print out structures like hashes so that you can ensure they are created properly. Use the command 'perldoc perldebug' to read the documentation when you get a chance.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://196695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-08-07 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?