Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 02-24-2002, 09:46 AM   #1
fab
Prospect
 
Join Date: Feb 2002
Location: Netherlands
Posts: 17
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
fab is offline   Reply With Quote
Old 02-24-2002, 10:49 AM   #2
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
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:

Code:
 
#!/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 is offline   Reply With Quote
Old 02-24-2002, 10:53 AM   #3
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
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!)

pmccann is offline   Reply With Quote
Old 02-24-2002, 11:46 AM   #4
fab
Prospect
 
Join Date: Feb 2002
Location: Netherlands
Posts: 17
Talking works like a charm! thanx!!

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
fab is offline   Reply With Quote
Old 02-25-2002, 06:51 AM   #5
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
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
pmccann is offline   Reply With Quote
Old 02-26-2002, 02:57 PM   #6
fab
Prospect
 
Join Date: Feb 2002
Location: Netherlands
Posts: 17
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
fab is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 06:06 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.