Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying an array 1

Status
Not open for further replies.

gepop

IS-IT--Management
May 9, 2009
3
0
0
IT
Hi to all, i just would copy the first n elements of an array into another. I initialize the first in a for loop, but when i go out of the loop it disappear. For what reason?

i tried so many manners, but i didn't get it. Do you have any idea?
 
It's probably a problem with scope. Hard to know without seeing your code.

To copy first 4 (for example) elements from one array to another:
Code:
@second = @first[0..3];
 
Code:
$num_pag=<STDIN>;

for ($i=0;$i<$num_pag*3;$i++)
{
@rif=int(rand $num_pag*2) +1;
}

@array2=@rif[0..3];
print @array2 ,"  \n";

this is the code.
 
probably should be:

Code:
$num_pag=<STDIN>;

for ($i=0;$i<$num_pag*3;$i++)
{
push @rif, int(rand $num_pag*2) +1;
}

@array2=@rif[0..3];
print @array2 ,"  \n";

Otherwise @rif will be a one element array after the loop is finished becaue you are using the assignment operator "=" to redefine the array entirely with each iteration of the "for" loop instead of using "push" to add to the array.

***Insert "strict" and "warnings" and proper variable declaration message here.***

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
what's the problem ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top