CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Problem in using split and join function!!

 



gevni
Novice

Nov 30, 2012, 10:15 AM

Post #1 of 13 (3372 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?

Code
# 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;


Code
# 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';


Code



Laurent_R
Veteran / Moderator

Nov 30, 2012, 2:11 PM

Post #2 of 13 (3367 views)
Re: [gevni] Problem in using split and join function!! [In reply to] Can't Post

Hi,

there is most probably a better way to do it, but I do not have time now to think about it and this should work:


Code
#!/usr/bin/perl  

use strict;
use warnings;

my @array= (2,2,2,2,3,3,4,4,4,1,1,1);

my ($previous, $series, @new_array);

foreach my $current (@array) {
unless (defined $previous) {
$previous = $current;
$series = $current;
next;
}
if ($previous == $current) {
$series .= "_" . $current;
}
else {
push @new_array, $series;
$series = $current
}
$previous = $current;
}
push @new_array, $series; # adds the last series found
print "@new_array"; # prints 2_2_2_2 3_3 4_4_4 1_1_1



gevni
Novice

Nov 30, 2012, 3:30 PM

Post #3 of 13 (3362 views)
Re: [Laurent_R] Problem in using split and join function!! [In reply to] Can't Post

Thanks. But for getting
@array= (2,2,2,2,3,3,4,4,4,1,1,1)
back from
@new_array={2_2_2_2 3_3 4_4_4 1_1_1}
I need to use split function on every element of array and it shows the same result instead of spliting into original.


Code
# For getting original array back  
foreach $elem (@new_array)
{
my @original= split('_ ', $elem);
push my @final, @original ;
}
print "@final";
Output:
2_2_2_2 3_3 4_4_4 1_1_1



Laurent_R
Veteran / Moderator

Dec 1, 2012, 12:28 AM

Post #4 of 13 (3339 views)
Re: [gevni] Problem in using split and join function!! [In reply to] Can't Post

Hi,

one possible way of resplitting the data:


Code
my  @new_array= qw/ 2_2_2_2 3_3 4_4_4 1_1_1 /; 
my @resplit = map {split /_/, $_} @new_array;
print "@resplit"; # prints "2 2 2 2 3 3 4 4 4 1 1 1"



gevni
Novice

Dec 1, 2012, 1:38 PM

Post #5 of 13 (3314 views)
Re: [Laurent_R] Problem in using split and join function!! [In reply to] Can't Post

But it only works when we already know the exact length and order of elements of array and pass it as a string.
I want to keep it generic. Like we have @new_array as an input with data elements like 2_2_2,3_3 etc. and we pass it to split function to get it like {2,2,2,3,3} .


Laurent_R
Veteran / Moderator

Dec 1, 2012, 3:02 PM

Post #6 of 13 (3312 views)
Re: [gevni] Problem in using split and join function!! [In reply to] Can't Post

Not at all, it is completely generic. Exactly the same code with a different input array:


Code
my  @new_array= qw/ 2_2_2_2 3_3 4_4_4 1_1_1 17_17_17 6_6_6_6_6 8_8/; 
my @resplit = map {split /_/, $_} @new_array;
print "@resplit";
# prints : 2 2 2 2 3 3 4 4 4 1 1 1 17 17 17 6 6 6 6 6 8 8


Please ask if you have trouble understanding the logic.


(This post was edited by Laurent_R on Dec 1, 2012, 3:08 PM)


gevni
Novice

Dec 1, 2012, 3:16 PM

Post #7 of 13 (3307 views)
Re: [Laurent_R] Problem in using split and join function!! [In reply to] Can't Post

I mean that you need to write the string in your code every time for new input string.
I want that

Code
input should be  @new_array  and we don't know what is inside.  mean we don't know the string. 
then we do
my @resplit = map {split /_/, $_} @new_array;
print "@resplit";

I want to avoid this qw/ 2_2_2_2 3_3 4_4_4 1_1_1 17_17_17 6_6_6_6_6 8_8/



gevni
Novice

Dec 1, 2012, 3:25 PM

Post #8 of 13 (3305 views)
Re: [gevni] Problem in using split and join function!! [In reply to] Can't Post

I get it. Sorry no need to reply. Thanks..


Laurent_R
Veteran / Moderator

Dec 2, 2012, 1:19 AM

Post #9 of 13 (3294 views)
Re: [gevni] Problem in using split and join function!! [In reply to] Can't Post

The @new_array array is generated earlier in your program (the initial transformation or your data and the generation of permutations), I have given it here only as a test example.


Rahul6990
Novice

Dec 27, 2012, 1:28 AM

Post #10 of 13 (3145 views)
Re: [gevni] Problem in using split and join function!! [In reply to] Can't Post

You can also hashes for this purpose:
my @array= (2,2,2,2,3,3,4,4,4,1,1,1);
my %output;
foreach(@array)
{
unless(defined($output{$_}))
{
$output{$_} = $_;
}
else
{
$output{$_} = $output{$_}."_".$_;
}
}
print Dumper(\%output);


BillKSmith
Veteran

Dec 27, 2012, 11:14 AM

Post #11 of 13 (3069 views)
Re: [Rahul6990] Problem in using split and join function!! [In reply to] Can't Post

Great idea, but not exactly equivalent to Laurent's solution. The result would be different if a sequence is repeated in the input.


Code
my @array= (2,2,2,2,3,3,4,4,4,1,1,1,2,2,2);


The specifications we have do not tell us if this can happen or how it should be handled. Your solution may actually be better.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Dec 27, 2012, 1:54 PM

Post #12 of 13 (3056 views)
Re: [BillKSmith] Problem in using split and join function!! [In reply to] Can't Post

This thread is actually a follow-up on one or possibly two previous threads in which the OP asked for a way to generate all permutations of the original data, but avoiding duplicates or equivalent values. This idea was to first join equivalent values into single scalars to create a much smaller array, generate the permutations on that smaller array, and then split the data again, so as to avoid generating a huge number of useless permutations and then filter out 99% of them. Although I don't remember all the details, I do not think a repeated sequence was possible.


Rahul6990
Novice

Jan 2, 2013, 10:26 AM

Post #13 of 13 (2664 views)
Re: [BillKSmith] Problem in using split and join function!! [In reply to] Can't Post

Thanks Bill.
Nice Point

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives