Solved

Perl array formatting

Posted on 2013-12-07
4
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

737 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