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
Solved

merging variables

Posted on 2000-04-18
6
164 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
  • 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 50 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

792 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