Re: split with a delimiter, every 4 time it occurs.
by davido (Cardinal) on Jul 01, 2013 at 07:20 UTC
|
Don't make it so hard. Don't craft some ugly regexp or confusing split. Just keep it simple; split on commas, grab five at a time, print them, move on.
use List::MoreUtils qw(natatime);
my $to_split = "'A','B','C','D','E','F','G','H','I','J','K','L','M','N
+','O','P','Q','R','S','T','U','V','W','X','Y','Z'";
my $it = natatime 5, split /,/, $to_split;
while( my @vals = $it->() ) {
local $" = ',';
print "@vals\t";
}
print "\n";
...the output...
'A','B','C','D','E' 'F','G','H','I','J' 'K','L','M','N','O' '
+P','Q','R','S','T' 'U','V','W','X','Y' 'Z'
| [reply] [d/l] [select] |
|
| [reply] |
|
There are several things that could be improved upon if memory is an issue. In fact, if memory is an issue, split probably shouldn't play any part in your solution, since it returns a list, which consumes a lot more space than the original string. natatime will be reasonably memory efficient, but expanding that nice, compact string into a list will be memory-expensive.
As for time, or computational efficiency (which is what you were asking about), it's an O(n) solution. Where you will get into trouble is where "n" grows large enough to send you into swap memory. There may be O(n) solutions with smaller per-iteration costs, but you won't ever turn this into O(log n) or O(1); you'll always be at least O(n).
If I were designing it with memory in mind, I would probably use index and substr together to find every fifth comma, and to extract the portion of the string that resides between every fifth comma. I would iterate one small segment at a time so that I'm never making a copy of a large string. If that approach turned out not to be fast enough, I would re-implement the same simple algorithm using Inline::C.
| [reply] [d/l] |
|
I would suggest that it really depends on how “very” your “big” is. You might have to read a large file in arbitrarily-sized sections, which can get messy when you are doing this sort of thing. Usually it’s best to cross such bridges if-and-when you get there. What sort of data-volume and speed requirements might you be dealing with?
| |
Re: split with a delimiter, every 4 time it occurs.
by davido (Cardinal) on Jul 01, 2013 at 06:37 UTC
|
Are you the same person who asked Seeking help with split!? The questions are close enough that the same solutions will apply, with minor modifications, which are left as an exercise for the student.
By the way, have you taken a look at the contents of $to_split? The way you're defining it, it will contain only 'A'.
| [reply] [d/l] [select] |
|
my $to_split = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N'
+,'O','P','Q','R','S','T','U','V','W','X','Y','Z';
my @ra = split m{ (?: \',\' [^\',\']+){50} \K \',\' }xms, $to_split ;
+
foreach my $piece(@ra){
print "\n\n" . $piece . "\n\n";
}
Can you pls suggest me where I got wrong?
Thanks in advance. | [reply] [d/l] |
|
| [reply] |
|
my $to_split = "'A','B','C','D','E','F','G','H','I','J','K','L','M','N
+'
+,'O','P','Q','R','S','T','U','V','W','X','Y','Z'";
Thanks | [reply] [d/l] |
Re: split with a delimiter, every 4 time it occurs.
by hdb (Monsignor) on Jul 01, 2013 at 07:10 UTC
|
Splitting can be done in other ways than using split. Here it is by far easier to describe the pieces that should result from the splitting and use a regex (a simple regex, relatively simple regex):
use strict;
use warnings;
my $to_split = "'A','B','C','D','E','F','G','H','I','J','K','L','M','N
+','O','P','Q','R','S','T','U','V','W','X','Y','Z'";
my @pieces = $to_split =~ /('\w'(?:,'\w'){0,4})/g;
$"="\n";
print "@pieces\n";
Or, if you insist on split, use the above as a delimiter, and remove the commas:
use strict;
use warnings;
my $to_split = "'A','B','C','D','E','F','G','H','I','J','K','L','M','N
+','O','P','Q','R','S','T','U','V','W','X','Y','Z'";
my @pieces = grep {$_} split /('\w'(?:,'\w'){0,4}),?/, $to_split;
$"="\n";
print "@pieces\n";
| [reply] [d/l] [select] |
|
I want this ','(not just comma) as the delimiter. I tried changein the coma to ',' but not working!
| [reply] |
Re: split with a delimiter, every 4 time it occurs.
by kcott (Archbishop) on Jul 01, 2013 at 06:44 UTC
|
$ perl -E 'say "Do your own work or learn nothing!";' > deaf.ears
| [reply] [d/l] |
Re: split with a delimiter, every 4 time it occurs.
by poj (Abbot) on Jul 01, 2013 at 06:46 UTC
|
You need put double quotes around the input string otherwise $to_split is just 'A'.
my $to_split = " 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'";
If you use warnings you would see messages like
Useless use of a constant ("B") in void context at scrap.pl line 4.
Useless use of a constant ("C") in void context at scrap.pl line 4.
Useless use of a constant ("D") in void context at scrap.pl line 4.
poj | [reply] [d/l] [select] |