Link to home
Start Free TrialLog in
Avatar of oostwijk
oostwijk

asked on

merging variables

I've got an array-variable called @yield in a sub called dorm1. @yield contains some variables seperated with comma's.

I've got a second array-variable called @yield in a sub called dorm2. This @yield array contains different variabled as in sub dorm1.

Now I want to have both the @yield array's (from both sub's) in one @yield array (the one from sub dorm2 pasted after the one from sub dorm1). Is this possible with one or more lines ?
Avatar of maneshr
maneshr

try this...

i am assuming that you are storing the yield array from dorm1 is stored in a and yield array from dorm2 in stored in b.

E.g.
@a=&dorm1;
@b=&dorm2;


#!/usr/local/bin/perl

## you can have any values in the array.
@a = (1, 3, 5, 6, 7, 8);
@b = (2, 3, 5, 7, 9);

## Try with these values too!!
##@a=("one","two","Three","nine");
##@b=("one","two","Three","four","five","seven","nine");

@arr=split(/,/,(join(',',@a).join(',',@b)));

print @arr,"\n";
==============================

in the future pl. post all your PERL related Q's to the PERL forum..

https://www.experts-exchange.com/Computers/Programming/Languages/Perl/

you will get a wider choice of answers.
ASKER CERTIFIED SOLUTION
Avatar of venky75
venky75

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It seems that I didn't explain my question well enough. This is what I've got in :

sub dorm1 {
$fld="dorm1";
@fields=(

);}
"1,11,2,4",
"1,12,6,7",
"1,13,4,4",
"1,4,6,7,5",
"1,5,6,66",
"1,6,6,5",
"1,7,2,1",
"9,19,1,1",
"9,20,1,22",
and this in:

sub dorm2 {
$fld="dorm2";
@fields=(
"3,11,2,4",
"1,12,6,1",
"5,13,7,4",
"6,4,6,5,5",
"1,8,6,6",
"1,6,6,4",
"1,8,2,1",
"9,19,6,3",
"9,20,3,1",
);}

I wan't to have the @field-arrays merged together so that I get a final array also called @field. (so that array looks like this :
@fields=(
"1,11,2,4",
"1,12,6,7",
"1,13,4,4",
"1,4,6,7,5",
"1,5,6,66",
"1,6,6,5",
"1,7,2,1",
"9,19,1,1",
"9,20,1,22",
"3,11,2,4",
"1,12,6,1",
"5,13,7,4",
"6,4,6,5,5",
"1,8,6,6",
"1,6,6,4",
"1,8,2,1",
"9,19,6,3",
"9,20,3,1",
);}

Is this possible with one or more lines ?


that comment came from me (oostwijk), I use the name turbobasic when I'm at my work.
try this...

#!/usr/local/bin/perl

sub dorm1 {
$fld="dorm1";
@fields=(
"1,11,2,4",
"1,12,6,7",
"1,13,4,4",
"1,4,6,7,5",
"1,5,6,66",
"1,6,6,5",
"1,7,2,1",
"9,19,1,1",
"9,20,1,22",
);}

sub dorm2 {
$fld="dorm2";
@fields=(
"3,11,2,4",
"1,12,6,1",
"5,13,7,4",
"6,4,6,5,5",
"1,8,6,6",
"1,6,6,4",
"1,8,2,1",
"9,19,6,3",
"9,20,3,1",
);}

@a=&dorm1;
@b=&dorm2;

@arr=(@a,@b);
print @arr,"\n";
OR try this....

#!/usr/local/bin/perl

sub dorm1 {
$fld="dorm1";
@fields=(
"1,11,2,4",
"1,12,6,7",
"1,13,4,4",
"1,4,6,7,5",
"1,5,6,66",
"1,6,6,5",
"1,7,2,1",
"9,19,1,1",
"9,20,1,22",
);}

sub dorm2 {
$fld="dorm2";
@fields=(
"3,11,2,4",
"1,12,6,1",
"5,13,7,4",
"6,4,6,5,5",
"1,8,6,6",
"1,6,6,4",
"1,8,2,1",
"9,19,6,3",
"9,20,3,1",
);}

@a=&dorm1;
@b=&dorm2;

@arr=split(/-/,join('-',@a,@b));

print scalar @arr,"\n";

foreach(@arr){
  print $_,"\n";
}