Avatar of TristinColby
TristinColby
 asked on

Perl Concatenate 2 arrays. Combining array1 element 1 and array 2 element 1 into a single string.

I'm trying to concatenate 2 arrays. The catch is that I'm trying to concatenate the elements. like the following

my @array = qw/one two three four/;
my @array2 = qw/a b c d/;

I want the result to be stored in a new array.
the result should be like:
one:a, two:b, three:c, four:d
Perl

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TristinColby

ASKER
Here's the original code: This works.
      Original Code:
      my $ref_svc = $dbh->selectcol_arrayref("SELECT Name from Services");
      my $ref_id = $dbh->selectcol_arrayref("SELECT ID from Services");
      my %hash = ();
     @hash{@{$ref_svc}} = @{$ref_id};

I realized i need to append a port on the end of the service name as i mentioned before so I tried this but it doesn't work and I'm stumped.

      my $ref_svc = $dbh->selectcol_arrayref("SELECT Name from Services");
      my $ref_id = $dbh->selectcol_arrayref("SELECT ID from Services");
      my $ref_port = $dbh->selectcol_arrayref("SELECT Port from Services");
      my %hash = ();
      my @tmp = ();
      @tmp = map{"@{$ref_svc}[$_]:@{$ref_port}[$_]"} $[..@#{$ref_svc};
      @hash{@tmp} = @{$ref_id};  
TristinColby

ASKER
Okay I figured it out. The problem is the way i'm getting the length of the array.  So how do i get the length of the array reference?
TristinColby

ASKER
figured it out.  @{$array}-1 works.

Thanks ozo.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ozo

or $#$array