Re: split n characters into n chunks
Hi, you can use substr $myword, 1,3 function Thanks, Mahesh On Sun, Oct 25, 2009 at 3:13 PM, Michael Alipio daem0n...@yahoo.com wrote: Hi, How do I split a word into n subsets? my $word = thequickbrown If I want three subsets I should be able to create: the heq equ upto own Using split function with limit of 3 gives me: t h equickbrown Any idea how to do this? I'm thinking maybe I can just split the whole string and push each character into array, then loop through the array, getting 3 elements set in the proces.. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- Thanks, MAhesh
Re: split n characters into n chunks
Shawn H Corey wrote: Dr.Ruud wrote: push @list, unpack x${_}a$size, $word for 0 .. $max; Funnily enough, that is somehowwhat faster than push @list, map unpack( x${_}a$size, $word ), 0 .. $max; You don't need the push: my @list = map unpack( x${_}a$size, $word ), 0 .. $max; Yes, been there, and was also slower. :) -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Shawn H Corey wrote: John W. Krahn wrote: $ perl -le' my $word = thequickbrown; my $subsets = 3; print for $word =~ /(?=(.{$subsets}))/g; Getting up there but substr is still the fastest. I had to set the iterations to 300_000, to get rid of warnings. $ perl5.8.8 3.pl Rate arrays match unpack match3 match2 unpack2 substr arrays 41265/s ---39%-40%-43%-44%-51%-73% match67114/s 63% -- -2% -8% -9%-20%-56% unpack 68337/s 66% 2% -- -6% -7%-19%-56% match3 72816/s 76% 8% 7% -- -1%-13%-53% match2 73350/s 78% 9% 7% 1% ---13%-52% unpack2 84034/s104% 25% 23% 15% 15% ---45% substr 153846/s273%129%125%111%110% 83% -- I moved some of the setup up, because I felt like it. unpack2() has less overhead than unpack. substr() mainly wins because it doesn't copy data. (I assume it just creates an extra SvP on (a part of) it) $ cat 3.pl #!/usr/bin/perl -w use strict; $| = 1; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; sub Testing() { 0 } use Benchmark qw(:all); my $word = thequickbrown; my $size = 3; Testing and print $_$/ for my $re_match = sprintf( .(?=%s), . x ($size-1) ), my $re_match2 = sprintf( (?=(%s)), . x $size ), ; my $max = length( $word ) - $size; if ( Testing ) { via_arrays(); via_substr(); via_unpack(); via_match(); via_match2(); via_unpack2(); } else { cmpthese( 300_000, { 'arrays' = \via_arrays, 'substr' = \via_substr, 'unpack' = \via_unpack, 'unpack2' = \via_unpack2, 'match' = \via_match, 'match2' = \via_match2, }); } sub via_arrays { my @list = (); my @array = split //, $word; push @list, join '', @array[ $_ .. $_ + $size - 1 ] for 0 .. $max; print Dumper \...@list if Testing; } sub via_substr { my @list = (); push @list, substr( $word, $_, $size ) for 0 .. $max; print Dumper \...@list if Testing; } sub via_unpack { my @list = (); push @list, (unpack( A${_}A$size, $word ))[1] for 0 .. $max; print Dumper \...@list if Testing; } sub via_unpack2 { my @list = (); push @list, unpack( x${_}a$size, $word ) for 0 .. $max; print Dumper \...@list if Testing; } sub via_match { my @list = (); push @list, substr( $word, $-[0], $size ) while $word =~ /$re_match/og; print Dumper \...@list if Testing; } sub via_match2 { my @list = (); push @list, $_ for $word =~ /$re_match2/og; print Dumper \...@list if Testing; } -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Shawn H Corey wrote: push @list, (unpack( A${i}A$size, $word ))[1]; Be careful with unpack A, because it rtrims. Best use x to skip, and a to capture. push @list, unpack x${_}a$size, $word for 0 .. $max; Funnily enough, that is somehowwhat faster than push @list, map unpack( x${_}a$size, $word ), 0 .. $max; -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Dr.Ruud wrote: Shawn H Corey wrote: push @list, (unpack( A${i}A$size, $word ))[1]; Be careful with unpack A, because it rtrims. Best use x to skip, and a to capture. push @list, unpack x${_}a$size, $word for 0 .. $max; Funnily enough, that is somehowwhat faster than push @list, map unpack( x${_}a$size, $word ), 0 .. $max; You don't need the push: my @list = map unpack( x${_}a$size, $word ), 0 .. $max; -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
split n characters into n chunks
Hi, How do I split a word into n subsets? my $word = thequickbrown If I want three subsets I should be able to create: the heq equ upto own Using split function with limit of 3 gives me: t h equickbrown Any idea how to do this? I'm thinking maybe I can just split the whole string and push each character into array, then loop through the array, getting 3 elements set in the proces.. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Michael Alipio wrote: Any idea how to do this? I'm thinking maybe I can just split the whole string and push each character into array, then loop through the array, getting 3 elements set in the proces.. Split the string into an array, loop through it and use a slice to join the elements. -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
On Sunday 25 Oct 2009 14:39:32 Shawn H Corey wrote: Michael Alipio wrote: Any idea how to do this? I'm thinking maybe I can just split the whole string and push each character into array, then loop through the array, getting 3 elements set in the proces.. Split the string into an array, loop through it and use a slice to join the elements. Why not use perldoc -f substr ( http://perldoc.perl.org/functions/substr.html ) in a loop? Alternatively one can use unpack but I'm not sure how well it would handle Unicode characters. Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ First stop for Perl beginners - http://perl-begin.org/ Chuck Norris read the entire English Wikipedia in 24 hours. Twice. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Shlomi Fish wrote: Why not use perldoc -f substr ( http://perldoc.perl.org/functions/substr.html ) in a loop? Alternatively one can use unpack but I'm not sure how well it would handle Unicode characters. You're right, substr works best. #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; use Benchmark qw(:all); my $word = thequickbrown; my $size = 3; cmpthese( 50_000, { 'via_arrays' = \via_arrays, 'via_substr' = \via_substr, 'via_unpack' = \via_unpack, }); # for testing only # via_arrays(); # via_substr(); # via_unpack(); sub via_arrays { my @array = split //, $word; my $max = @array - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, join '', @array[ $i .. $i+$size-1 ]; } # print Dumper \...@list; #for testing only } sub via_substr { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, substr( $word, $i, $size ); } # print Dumper \...@list; #for testing only } sub via_unpack { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, (unpack( A${i}A$size, $word ))[1]; } # print Dumper \...@list; #for testing only } -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Michael Alipio wrote: my $word = thequickbrown If I want three subsets I should be able to create: the heq equ . upto . own print substr( $word, $-[0], 3 ) while $word =~ /.(?=..)/g; -- Ruud -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Dr.Ruud wrote: print substr( $word, $-[0], 3 ) while $word =~ /.(?=..)/g; Doesn't beat substr. #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; use Benchmark qw(:all); my $word = thequickbrown; my $size = 3; cmpthese( 50_000, { 'via arrays' = \via_arrays, 'via substr' = \via_substr, 'via unpack' = \via_unpack, 'via match' = \via_match, }); # for testing only # via_arrays(); # via_substr(); # via_unpack(); # via_match(); sub via_arrays { my @array = split //, $word; my $max = @array - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, join '', @array[ $i .. $i+$size-1 ]; } # print Dumper \...@list; #for testing only } sub via_substr { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, substr( $word, $i, $size ); } # print Dumper \...@list; #for testing only } sub via_unpack { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, (unpack( A${i}A$size, $word ))[1]; } # print Dumper \...@list; #for testing only } sub via_match { my @list = (); push @list, substr( $word, $-[0], 3 ) while $word =~ /.(?=..)/g; #print Dumper \...@list; #for testing only } -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Michael Alipio wrote: Hi, Hello, How do I split a word into n subsets? my $word = thequickbrown If I want three subsets I should be able to create: the heq equ upto own $ perl -le' my $word = thequickbrown; my $subsets = 3; print for $word =~ /(?=(.{$subsets}))/g; ' the heq equ qui uic ick ckb kbr bro row own John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
John W. Krahn wrote: $ perl -le' my $word = thequickbrown; my $subsets = 3; print for $word =~ /(?=(.{$subsets}))/g; Getting up there but substr is still the fastest. #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; my $Testing = $ARGV[0] || 0; use Benchmark qw(:all); my $word = thequickbrown; my $size = 3; if( $Testing ){ via_arrays(); via_substr(); via_unpack(); via_match(); via_match2(); }else{ cmpthese( 50_000, { 'via arrays' = \via_arrays, 'via substr' = \via_substr, 'via unpack' = \via_unpack, 'via match' = \via_match, 'via match2' = \via_match2, }); } sub via_arrays { my @array = split //, $word; my $max = @array - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, join '', @array[ $i .. $i+$size-1 ]; } # print Dumper \...@list; #for testing only } sub via_substr { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, substr( $word, $i, $size ); } # print Dumper \...@list; #for testing only } sub via_unpack { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, (unpack( A${i}A$size, $word ))[1]; } # print Dumper \...@list; #for testing only } sub via_match { my @list = (); push @list, substr( $word, $-[0], 3 ) while $word =~ /.(?=..)/g; # print Dumper \...@list; #for testing only } sub via_match2 { my @list = (); push @list, $_ for $word =~ /(?=(.{$size}))/g; # print Dumper \...@list; #for testing only } -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
Shawn H Corey wrote: John W. Krahn wrote: $ perl -le' my $word = thequickbrown; my $subsets = 3; print for $word =~ /(?=(.{$subsets}))/g; Getting up there but substr is still the fastest. #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; my $Testing = $ARGV[0] || 0; use Benchmark qw(:all); my $word = thequickbrown; my $size = 3; if( $Testing ){ via_arrays(); via_substr(); via_unpack(); via_match(); via_match2(); }else{ cmpthese( 50_000, { 'via arrays' = \via_arrays, 'via substr' = \via_substr, 'via unpack' = \via_unpack, 'via match' = \via_match, 'via match2' = \via_match2, }); } sub via_arrays { my @array = split //, $word; my $max = @array - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, join '', @array[ $i .. $i+$size-1 ]; } # print Dumper \...@list; #for testing only } sub via_substr { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, substr( $word, $i, $size ); } # print Dumper \...@list; #for testing only } sub via_unpack { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, (unpack( A${i}A$size, $word ))[1]; } # print Dumper \...@list; #for testing only } sub via_match { my @list = (); push @list, substr( $word, $-[0], 3 ) while $word =~ /.(?=..)/g; # print Dumper \...@list; #for testing only } sub via_match2 { my @list = (); push @list, $_ for $word =~ /(?=(.{$size}))/g; Why the for loop? my @list = $word =~ /(?=(.{$size}))/g; # print Dumper \...@list; #for testing only } John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: split n characters into n chunks
John W. Krahn wrote: Why the for loop? my @list = $word =~ /(?=(.{$size}))/g; # print Dumper \...@list; #for testing only } Because you sent it with a loop. It also seems faster. #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; my $Testing = $ARGV[0] || 0; use Benchmark qw(:all); my $word = thequickbrown; my $size = 3; if( $Testing ){ via_arrays(); via_substr(); via_unpack(); via_match(); via_match2(); via_match3(); }else{ cmpthese( 50_000, { 'via arrays' = \via_arrays, 'via substr' = \via_substr, 'via unpack' = \via_unpack, 'via match' = \via_match, 'via match2' = \via_match2, 'via match3' = \via_match3, }); } sub via_arrays { my @array = split //, $word; my $max = @array - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, join '', @array[ $i .. $i+$size-1 ]; } # print Dumper \...@list; #for testing only } sub via_substr { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, substr( $word, $i, $size ); } # print Dumper \...@list; #for testing only } sub via_unpack { my $max = length( $word ) - $size; my @list = (); for my $i ( 0 .. $max ){ push @list, (unpack( A${i}A$size, $word ))[1]; } # print Dumper \...@list; #for testing only } sub via_match { my @list = (); push @list, substr( $word, $-[0], 3 ) while $word =~ /.(?=..)/g; # print Dumper \...@list; #for testing only } sub via_match2 { my @list = (); push @list, $_ for $word =~ /(?=(.{$size}))/g; # print Dumper \...@list; #for testing only } sub via_match3 { my @list = $word =~ /(?=(.{$size}))/g; # print Dumper \...@list; #for testing only } -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. I like Perl; it's the only language where you can bless your thingy. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/