Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Empyting an array inot multiple arrays

by pr09 (Novice)
on Oct 27, 2011 at 14:57 UTC ( #934165=perlquestion: print w/ replies, xml ) Need Help??
pr09 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a scenario where i have an array say @array_original with 300 elements. Not i have to empty the array such that there are 3 new arrays array1,array2,array3. @array1 should have array elements from 1..100. @array2 from 101..201. @array3 from 202..300 Can anyone tell me how to implement this scenario.

Comment on Empyting an array inot multiple arrays
Re: Empyting an array inot multiple arrays
by Anonymous Monk on Oct 27, 2011 at 15:02 UTC
Re: Empyting an array inot multiple arrays
by Lotus1 (Chaplain) on Oct 27, 2011 at 15:42 UTC

    Remember that 'element 1' will have an index of zero. There won't be an index 300. The indexes will be 0-299.

      Yes the basic idea is the new arrays should not hold more than 100 elements,when it crosses 100 the elements should be copied to a new array.
Re: Empyting an array inot multiple arrays
by clueless newbie (Hermit) on Oct 27, 2011 at 17:20 UTC
    Offered for consideration --- Note it neither really creates the arrays @array1,@array2,@array3 nor does it really clear @array_original.
    #!/usr/bin/perl use Data::Alias; use Data::Dumper; use strict; use warnings; my @Array; $Array[$_]=$_ for (0..29); alias my @Array_1=@Array[0..9]; alias my @Array_2=@Array[10..19]; alias my @Array_3=@Array[20..29]; warn Data::Dumper->Dump([\@Array,\@Array_1,\@Array_2,\@Array_3],[qw(*A +rray *Array_1 *Array_2 *Array_3)]);
Re: Empyting an array inot multiple arrays
by aaron_baugher (Deacon) on Oct 27, 2011 at 19:35 UTC
    I realize this is probably homework, but you shouldn't do this. Any time you find yourself naming variables var1, var2, var3, etc., that's a very good sign that they should be in some kind of array or hash. If there were ever a reason to break a large array up into smaller ones and have them numbered, you'd want to break it up into @{$array[0]}, @{$array[1]}, @{$array[2]}, etc. An array of arrays, with each sub-array limited to whatever size you like, but with them organized inside a top-level array so you don't have to start doing goofy stuff with eval or globs to get the variable names numbered the way you like.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://934165]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-12-27 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls