Link to home
Create AccountLog in
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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of TristinColby
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};  
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?
figured it out.  @{$array}-1 works.

Thanks ozo.
or $#$array