
gevni
Novice
Nov 30, 2012, 10:15 AM
Post #1 of 13
(7398 views)
|
Problem in using split and join function!!
|
Can't Post
|
|
Hi i have an array for example (2,2,2,2,3,3,4,4,4,1,1,1) i want to convert it into this (2_2_2_2, 3_3 ,4_4_4, 1_1_1) by using join and then get back the original one by using split on every element of array. The code that i write is giving some error. Can you plz help me what i am doing wrong?
# Join the same value elements in single element of array use Data::Dumper; my @array= (2,2,2,2,3,3,4,4,4,1,1,1); my @groups; my $group; my $i=0; my $j=0; while ($i<$#array) { $group="$array[$i]"; $i++; while ($array[$i]== $array[($i-1)]) { $group = join "_ ", $array[$i]; $i++; } push @groups, $group; } print Dumper @groups; # For getting original array back foreach $elem (@groups) { my @colour_1= split('_ ', $elem); push @colors, @colour_1 ; } print Dumper @colors; Dumper print output: Use of uninitialized value in numeric eq (==) at ./transform_array.pl line 22. $VAR1 = '2'; $VAR2 = '3'; $VAR3 = '4'; $VAR4 = '1';
|