Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl array formatting

Posted on 2013-12-07
4
Medium Priority
?
233 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

618 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