Solved

Perl array formatting

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

11 Experts available now in Live!

Get 1:1 Help Now