?
Solved

merging variables

Posted on 2000-04-18
6
Medium Priority
?
171 Views
Last Modified: 2013-12-25
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 ?
0
Comment
Question by:oostwijk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2728193
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..

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

you will get a wider choice of answers.
0
 

Accepted Solution

by:
venky75 earned 200 total points
ID: 2729331
Hi there,

I would like to correct something in what maneshr has posted.

It would be something like this:

@y1=&dorm1;
@y2=&dorm2;
@yield=(@y1,@y2);

sub dorm1
{
 @yield1=("a","b","c");
 #rest of the code
 return @yield1;
}

sub dorm2
{
 @yield2=("d","e","f")
 #rest of the code
 return @yield2;
}

Hope this works out for u.

Bye
venky


0
 

Expert Comment

by:turbobasic
ID: 2731477
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 ?


0
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 

Expert Comment

by:turbobasic
ID: 2731486
that comment came from me (oostwijk), I use the name turbobasic when I'm at my work.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2731539
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";
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2731556
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";
}
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

801 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