Re: split n characters into n chunks

2009-10-27 Thread Dr.Ruud
Shawn H Corey wrote: Dr.Ruud wrote: push @list, unpack "x${_}a$size", $word for 0 .. $max; Funnily enough, that is somehow&what 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;

Re: split n characters into n chunks

2009-10-27 Thread mahesh bhasme
Hi, you can use substr $myword, 1,3 function Thanks, Mahesh On Sun, Oct 25, 2009 at 3:13 PM, Michael Alipio 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 > >

Re: split n characters into n chunks

2009-10-26 Thread Shawn H Corey
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 som

Re: split n characters into n chunks

2009-10-26 Thread Dr.Ruud
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 somehow&what faster than push @list, map

Re: split n characters into n chunks

2009-10-26 Thread Dr.Ruud
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

Re: split n characters into n chunks

2009-10-25 Thread Shawn H Corey
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

Re: split n characters into n chunks

2009-10-25 Thread John W. Krahn
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::Du

Re: split n characters into n chunks

2009-10-25 Thread Shawn H Corey
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 =

Re: split n characters into n chunks

2009-10-25 Thread John W. Krahn
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 he

Re: split n characters into n chunks

2009-10-25 Thread Shawn H Corey
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

Re: split n characters into n chunks

2009-10-25 Thread Dr.Ruud
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,

Re: split n characters into n chunks

2009-10-25 Thread Shawn H Corey
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

Re: split n characters into n chunks

2009-10-25 Thread Shlomi Fish
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 int

Re: split n characters into n chunks

2009-10-25 Thread Shawn H Corey
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.

split n characters into n chunks

2009-10-25 Thread Michael Alipio
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 st