Solved

Perl array formatting

Posted on 2013-12-07
4
223 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now