1

My array will have strings as below...

my @array1 = ( "CE_2N_Comp_ChmProcess",
             "CE_2N_Comp_FmMasterProcess"....
             "CE_2N_Comp_EmaProcess" );

And I want to use array1 values as name of new arrays... e.g.,

my @CE_2N_Comp_ChmProcess = (1,2,3);
my @CE_2N_Comp_FmMasterProcess = (4,5,6);
.
.
my @CE_2N_Comp_EmaProcess = (7,8,9);

Please help.

Expected array names

my @CE_2N_Comp_ChmProcess = (1,2,3);
my @CE_2N_Comp_FmMasterProcess = (4,5,6);
.
.
my @CE_2N_Comp_EmaProcess = (7,8,9);
3
  • Can you share what you have done to get the desired output Commented Jan 18, 2023 at 15:48
  • 7
    This is a bad idea in many ways. Don't do it. Perl has perfectly good data structures. You can access things by name using a hash. Don't use the variable namespace as if it was a hash. Commented Jan 18, 2023 at 15:48
  • 1
    Re "And I want to use array1 values as name of new arrays", No, you don't Commented Jan 18, 2023 at 16:44

1 Answer 1

1

In reply (Perl console), using a HASH with ARRAYs data structure:

> my $h = {} # create a HASH ref
$res[1] = {}

> push @{ $h->{CE_2N_Comp_ChmProcess} }, (1,2,3);
$res[2] = 3

> push @{ $h->{CE_2N_Comp_FmMasterProcess} }, (4,5,6);
$res[3] = 3

> push @{ $h->{CE_2N_Comp_EmaProcess} }, (7,8,9);
$res[4] = 3

> use Data::Dumper;
> print Dumper $h
{
  'CE_2N_Comp_ChmProcess' => [
                               1,
                               2,
                               3
                             ],
  'CE_2N_Comp_EmaProcess' => [
                               7,
                               8,
                               9
                             ],
  'CE_2N_Comp_FmMasterProcess' => [
                                    4,
                                    5,
                                    6
                                  ]
}

Or used dynamically :

0> my $h = {}
$res[0] = {}

1> my $c = 0;
$res[1] = 0

2> for ("CE_2N_Comp_ChmProcess", "CE_2N_Comp_FmMasterProcess", "CE_2N_Comp_EmaProcess") { \
    push @{ $h->{$_} }, (++$c .. ($c+2)); $c+=2 }
$res[2] = ''

3> use Data::Dumper;
4> print Dumper $h;
{
  'CE_2N_Comp_ChmProcess' => [
                               1,
                               2,
                               3
                             ],
  'CE_2N_Comp_EmaProcess' => [
                               7,
                               8,
                               9
                             ],
  'CE_2N_Comp_FmMasterProcess' => [
                                    4,
                                    5,
                                    6
                                  ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

But the problem is I never know the Array (Hash key name in your case) name, it is dynamically received from remote end. Below array values (CE_2N_Comp.... etc) are dynamically received... I want to name these values as new array names my @array1 = ( "CE_2N_Comp_ChmProcess", "CE_2N_Comp_FmMasterProcess".... "CE_2N_Comp_EmaProcess" );
So edit your question to better explain what the input looks like, how it's formated, where the list comes from and how to map them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.