|
Prev: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.8 $)
Next: FAQ 1.13 Is it a Perl program or a Perl script?
From: Ben Bullock on 22 Apr 2008 11:30 On Tue, 22 Apr 2008 16:24:19 +0200, Hartmut Camphausen wrote: > Prints [1, Fred, 24, m] > [2, Frank, 42, m] > [id, name, age, sex] Yes, I know. So what? > Take care of the 'sort' ;-) I don't care about the order of these results. The original poster can figure out all the details if he wants to. > Alternatively, if you want to take care of the IDs of /both/ arrays, you > could write: Yes, but I have no way to really guess in detail what the original poster wanted, so why spend a lot of time trying to cover every possibility? I wrote that initial answer in only a minute or two, and I think it's good enough to get the poster on track towards an answer.
From: nolo contendere on 22 Apr 2008 11:51 On Apr 22, 11:11 am, nolo contendere <simon.c...(a)fmr.com> wrote: > On Apr 22, 2:35 am, Vishal G <v3gu...(a)gmail.com> wrote: > > > > > Hi Guys, > > > I have a little complicated problem... > > > I have two arrays > > > @a = ( ['id', 'name', 'age'], > > ['1', 'Fred', '24'], > > ['2', 'Frank', '42'], > > ); > > > @b = ( ['id', 'sex'], > > ['1', 'm' ], > > ['2', 'm'], > > ); > > > I want to join these two AoA, based on id, so the resulting array will > > look like this > > > @c = ( ['id', 'name', 'age', 'sex'], > > ['1', 'Fred', '24', 'm' ], > > ['2', 'Frank', '42', 'm'], > > ); > > > Any Ideas? > > I'm not sure how much control you have over the structure of the data, > but it seems to me that you would be better served if the data were > stored in hashes to begin with: > > use strict; use warnings; > use Data::Dumper; > > my %people = ( > 1 => { name => 'Fred', age => 24 }, > 2 => { name => 'Frank', age => 42 }, > ); > > my %gender = ( > 1 => { sex => 'm' }, > 2 => { sex => 'm' }, > ); > > ...then it's a simple matter to combine the two structures, keyed by > the id. > > for my $id ( keys %people ) { > $people{$id}{sex} = $gender{$id}{sex}; > > } > > print Dumper( \%people ), "\n"; One more thing, are you certain that you wanted to quote the age values in your data? in most cases perl is smart enough to dwym, but for clarity i think it would be better to leave them as numbers rather than strings.
From: benkasminbullock on 22 Apr 2008 23:49 nolo contendere <simon.chao(a)fmr.com> wrote: > I'm not sure how much control you have over the structure of the data, > but it seems to me that you would be better served if the data were > stored in hashes to begin with: If the person needs to know how to put in a screw, why tell him he would be better served by using nails and a hammer? A quote from Stephen King (viewable at http://www.nsftools.com/blog/blog-03-2006.htm) comes to mind: "I want to suggest to you that to write to the best of your abilities, it behooves you to construct your own toolbox and then build up enough muscle so you can carry it with you. Then, instead of looking at a hard job and getting discouraged, you will perhaps seize the correct tool and get immediately to work." In the real world, there are lots of situations in which you're likely to get data in an array of arrays. For example if you take data from a "Range" in an Excel spreadsheet, you'll get it back as an array of arrays, just like the original poster's problem. Instead of looking at a hard job and getting discouraged, I'd suggest you think about seizing the correct tool and getting immediately to work.
From: Keith Keller on 23 Apr 2008 00:55 On 2008-04-23, benkasminbullock(a)gmail.com <benkasminbullock(a)gmail.com> wrote: > > A quote from Stephen King (viewable at > http://www.nsftools.com/blog/blog-03-2006.htm) comes to mind: > > "I want to suggest to you that to write to the best of your abilities, > it behooves you to construct your own toolbox and then build up enough > muscle so you can carry it with you. Then, instead of looking at a > hard job and getting discouraged, you will perhaps seize the correct > tool and get immediately to work." use Shining; my $shine=Shining->new; $shine->enterRoom(237); $shine->avoid('Dad'); $shine->call('cook'); $shine->REDRUM; $shine->escape('hedgeMaze','snowmobile'); --keith --keith -- kkeller-usenet(a)wombat.san-francisco.ca.us (try just my userid to email me) AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt see X- headers for PGP signature information
From: Ted Zlatanov on 23 Apr 2008 09:22 On Tue, 22 Apr 2008 21:55:03 -0700 Keith Keller <kkeller-usenet(a)wombat.san-francisco.ca.us> wrote: KK> use Shining; KK> my $shine=Shining->new; KK> $shine->enterRoom(237); KK> $shine->avoid('Dad'); KK> $shine->call('cook'); KK> $shine->REDRUM; KK> $shine->escape('hedgeMaze','snowmobile'); I think you forgot to include the POD: All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy All work and no play makes Jack a dull boy
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.8 $) Next: FAQ 1.13 Is it a Perl program or a Perl script? |