Re: Split at specific spaces
by BrowserUk (Patriarch) on May 11, 2010 at 07:13 UTC
|
Is there any way I can perform the above?
Not particularly efficient, but possible:
$s = '1 AC2 34 TRP A 6 ALA A 7 ILE A 14 GLY A 15';;
print for grep length, split /(\S+\s+\S+\s+\S+)\s+/, $s;;
1 AC2 34
TRP A 6
ALA A 7
ILE A 14
GLY A 15
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: Split at specific spaces
by rubasov (Friar) on May 11, 2010 at 08:15 UTC
|
Another way returning the split parts only:
perl -E 'say for split /\S+\s+\S+\s+\S+\K\s+/, "1 2 3 4 5 6 7 8 9"'
Or written shorter:
perl -E 'say for split /(?:\S+\K\s+){3}/, "1 2 3 4 5 6 7 8 9"'
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
| [reply] |
|
\K Keep the stuff left of the \K, don't include it in $&
| [reply] [d/l] |
Re: Split at specific spaces
by toolic (Bishop) on May 11, 2010 at 13:05 UTC
|
use strict;
use warnings;
use List::MoreUtils qw(natatime);
my $line = '1 AC2 34 TRP A 6 ALA A 7 ILE A 14 GLY A 15';
my @tokens = split /\s+/, $line;
my $it = natatime(3, @tokens);
while (my @vals = $it->()) {
print "@vals\n";
}
| [reply] [d/l] [select] |
Re: Split at specific spaces
by johngg (Canon) on May 11, 2010 at 12:08 UTC
|
$ perl -E '
$_ = q{1 AC2 34 TRP A 6 ALA A 7 ILE A 14 GLY A 15};
say for m{(?: (?<=\A) | (?<=\s) )( (?:\S+\s+){2} \S+ ) }gx;'
1 AC2 34
TRP A 6
ALA A 7
ILE A 14
GLY A 15
$
I hope this is useful.
| [reply] [d/l] |
Re: Split at specific spaces
by Cristoforo (Curate) on May 11, 2010 at 16:28 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
$_ = '1 AC2 34 TRP A 6 ALA A 7 ILE A 14 GLY A 15';
print join "\n", /\S+(?: \S+){1,2}/g;
Update: above solution better as:
print "$1\n" while /(\S+(?:\s+\S+){0,2})/g; | [reply] [d/l] [select] |
Re: Split at specific spaces
by johngg (Canon) on May 11, 2010 at 23:05 UTC
|
Yet another way using split on spaces but with a capture so the spaces are retained. Discard every 6th element then join five at a time.
$ perl -E '
> $_ = q{1 AC2 34 TRP A 6 ALA A 7 ILE A 14 GLY A 15};
> @e = grep { ( ++ $i ) % 6 } split m{(\s+)};
> say join q{}, map { shift @e } 1 .. 5 while @e;'
1 AC2 34
TRP A 6
ALA A 7
ILE A 14
GLY A 15
$
| [reply] [d/l] |
|
$name=qq(1 AC2 34 TRP A 6 ALA A 7 ILE A 14 GLY A 15);
split (/ /, $name);
### Process the temp array
for ( $i=0; $i<=$#_; $i++)
{
push @get, (join ' ', @_[$i..$i+2]);
$i=$i+2;
}
print "\n$_" for @get;
| [reply] [d/l] |
|
While this code does produce correct results in this case, you are ignoring a warning message because you do not use strict and warnings:
Use of implicit split to @_ is deprecated
| [reply] [d/l] |
Re: Split at specific spaces
by Anonymous Monk on May 13, 2010 at 11:58 UTC
|
doesn't seem that difficult.
$ perl -le 'use strict;use warnings;my($s,@r,@t) = "1 AC2 34 TRP A 6 A
+LA A 7 ILE A 14 GLY A 15";@t=split/\s+/,$s;while((@r=splice @t,0,3)==
+3){print join(" ",@r);}warn"leftovers: @r\n"if @r;'
1 AC2 34
TRP A 6
ALA A 7
ILE A 14
GLY A 15
what's the catch? | [reply] [d/l] |