View Full Version : Perl Question from newbie
I've been using the following code with success, but I'd like to do more elegant it with a loop...but somehow I can't get it to work...
push @select, {"$headers[0]" => "$m_e[0]",
"$headers[1]" => "$m_e[1]",
"$headers[2]" => "$m_e[2]",
"$headers[3]" => "$m_e[3]",
"$headers[4]" => "$m_e[4]",
"$headers[5]" => "$m_e[5]",
"$headers[6]" => "$m_e[6]",
"$headers[7]" => "$m_e[7]",
"$headers[8]" => "$m_e[8]",
"$headers[9]" => "$m_e[9]",
"$headers[10]" => "$m_e[10]",
"$headers[11]" => "$m_e[11]",
"$headers[12]" => "$m_e[12]",
"$headers[13]" => "$m_e[13]"};
The goal is that the number of header items should be variable, so $#headers would set the loop iteration. Now I have to do this kind of thing manually, like adding "$headers[14]" => "$m_e[14]"...
Any suggestion is welcome...I've tried a lot of loop options/constructions...but no luck...
Thanks,
Fabien
pmccann
02-24-2002, 10:49 AM
So you've got an array @headers and another array @m_e and you're wanting to "interleave" their contents into an anonymous hash? (Hmm, if so you're not *that* much of a newbie!!).
How about something like:
my %hashed = map {$headers[$_],$m_e[$_]} 1..$#headers;
push @select,\%hashed;
But you should note that %hashed looks a bit redundant here, so we should be able to flick the switch on its sorry existence via:
push @select, {map{$headers[$_],$m_e[$_]} 1..$#headers};
If I've misunderstood what you're trying to do please let me know!
Cheers,
Paul
For what it's worth, here's my mini-test program:
#!/usr/bin/perl -w
use strict;
my @headers=1..15;
my @m_e = 'a'..'z';
my @select;
push @select, {map{$headers[$_],$m_e[$_]} 0..$#headers};
# Now check what's hiding in that hash!! First the keys...
my $out=join ":", sort {$a <=> $b} keys %{$select[0]};
print $out,"\n";
## Prints... 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15
# And now the values:
my $out=join ":", sort values %{$select[0]};
print $out,"\n";
## Prints... a:b:c:d:e:f:g:h:i:j:k:l:m:n:o
# Finally, just check that the right key corresponds to the right value:
while (my ($key,$value) = each(%{$select[0]})){
print "key $key has value $value\n";
}
# Output suppressed, just run this script to see it! Works as expected...
pmccann
02-24-2002, 10:53 AM
Damn: attack of the killer smiley!! Forgot to click that box again (and too lazy to edit the post directly. Shame, shame, shame, shame, shame, shame.... SHAME ON ME!)
:cool:
Wow, your solution works right out of the box ;-)
didn't have to change a thing! Given the limited info I gave you, I'd have to say that was a bit surprised to find such a smooth solution (especialy since I've been trying for a couple of hours...)
Great!
Fabien
pmccann
02-25-2002, 06:51 AM
You're more than welcome: I really *like* finding solutions to problems like that!
"map" is a very cool function, and well worth keeping in mind when you're doing anything with arrays or hashes (which you can treat as arrays when desired). It's most useful in concert with "grep". I can't resist a nice tidbit that extracts the unique elements out of an array @words and puts them in another array @uniques
my @uniques = grep {$count_hash{$_}==1} map {$count_hash{$_}++;$_} @words;
Of course if you're after the repeated items in @words just change the "==1" to ">1", and so on.
Best wishes,
Paul
thanx again! now that I got CGI support enabled in Apache, everything is running smoothly ;-)
I will keep an eye out on 'map'...I guess I'd better read some more Perl books then...
Fabien
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.