Solved

Perl array formatting

Posted on 2013-12-07
4
227 Views
Last Modified: 2013-12-07
I have spent a lot of time and can not get this to work seems very simple yet I can not find the answer on my own.

I have 2 arrays that are populated by a perl module.  One contains names of countries the other contains a country code. Here is my code

#!/usr/bin/env perl

use strict;
use warnings;

use Locale::Country;

my @names = all_country_names();
my @codes = all_country_codes(LOCALE_CODE_ALPHA_2);

use Data::Dumper qw(Dumper);
print Dumper \@codes;

Open in new window



the output of @codes looks like this same for @names

@code
$VAR1 = [
          'ad',
          'ae',
          'af',
....
];

@names
$VAR1 = [
          'AFGHANISTAN',
          'Aland Islands',
          'ALBANIA',
....
];

Open in new window




What I want to be able to do is merge these 2 together so it looks like

$VAR1 = [ 'ad', 'AFGHANISTAN' ]
$VAR2 = [ 'ae', 'Aland Islands ]

Open in new window


tried push but that just appends them.  My goal is to be able to import them

$schema->populate('Table_name', [
  [ 'code', 'name' ],
  @mergedarray,
]);

Open in new window

0
Comment
Question by:hexfusion
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39703706
my %merge;
my @codes = all_country_codes(LOCALE_CODE_ALPHA_2);
@merge{@codes}=all_country_names();
my  @mergedarray=map[$_,$merge{$_}],@codes;
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39703707
How did you try push?
If you don't need to preserve @names, you could do something like
  push @mergedarray,[$_,shift @names]for @codes;
If you don't need to preserve either, you might  do something like
  push @mergedarray,[shift @codes,shift @names]while @codes;
If you need to preserve both you might do
  push @mergedarray,[$codes[$_],$names[$_]] for 0..$#codes;  
or
  @mergedarray=map[$codes[$_],$names[$_]],0..$#codes;
0
 
LVL 2

Author Comment

by:hexfusion
ID: 39703853
push @mergedarray,[shift @codes,shift @names]while @codes; 

Open in new window


yes perfect thank you soo very much.
0
 
LVL 2

Author Closing Comment

by:hexfusion
ID: 39703855
Thanks again that was great
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question