Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

Perl - Array manipulation

Perl question:  How can I re-position a specific number of x records in an array (if my array contained 10 records of x & y data) as shown below.  For example, I would like to reposition a specific number of x-records (ie: 1-5 records) moving them to the end of the x column of the array, leaving the y column without change as new_array (below) shows?  

Thusfar: I've used push @array, splice($array[0],0,5); which moves 5 records of both x-y data.  
  Array
  X      Y
  1       2
  2       3
  3       4
  4       5
  5       6
  6       7
  7       8
  8       9
  9      10
  10    11
Output from new_Array
X   Y
6   2
7   3
8   4
9   5
10  6
1   7
2   8
3   9
4   10
5   11
0
greenpath
Asked:
greenpath
1 Solution
 
ozoCommented:
@array = map[split],<DATA>;
sub transpose { map { my $i = $_; [ map $_->[ $i ], @_ ] } 0 .. $#{ $_[0] } };
@array = transpose @array ;
push @{$array[0]}, splice(@{$array[0]},0,5);
@array = transpose @array ;
print "@$_\n" for @array;

__DATA__
  1       2
  2       3
  3       4
  4       5
  5       6
  6       7
  7       8
  8       9
  9      10
  10    11
0
 
greenpathAuthor Commented:
Yes- thanks for the help!  Your solution works well, a bit cryptic and not too easy to read but does the answer my question.  
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now