Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2007, 06:24 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default build array from multiple values perl

Does anyone know if there is an easier way to do this? Maybe in a foreach loop or something like that.

Code:
push @basis, { "2" => "2 Adults" };
push @basis, { "3" => "3 Adults" };
push @basis, { "4" => "4 Adults" };
push @basis, { "2-1" => "2 Adults + 1 Child" };
push @basis, { "2-2" => "2 Adults + 2 Children" };
It just seems a long-winded way to code it into an array.

Picco

www.crmpicco.co.uk
www.ie7.com
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old May 10th, 2007, 11:19 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You could at least dispense with the pushes and declare the array in one go.
Code:
my %basis = (
        "2" => "2 Adults",
        "3" => "3 Adults",
        "4" => "4 Adults",
        "2-1" => "2 Adults + 1 Child",
        "2-2" => "2 Adults + 2 Children",
);

print "$_ => $basis{$_}\n" for (sort keys %basis);
Or, if you want a programmatic way to do it:

Code:
my %basis = map {$_=>make_str($_)} ('2','3','4','2-1','2-2');

sub make_str {
       my ($adults,$kids)= split/-/, shift;
       return "$adults Adults + $kids Kids" if $kids;
       return "$adults Adults";
}
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Pivot Table Build Issue baycolor Excel VBA 0 March 21st, 2008 10:15 AM
How do I add up values in an array? Lucy Classic ASP Basics 6 May 31st, 2005 05:26 AM
Build an array dynamically yeva40 Javascript How-To 1 May 27th, 2005 03:09 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.