I have a line like this:
a b c d
I process my string as follows:
chomp $line; my @tokens = split /\s+/, $line; my @new_tokens; foreach my $token (@tokens) { push @new_tokens, some_complex_function( $token ); } my $new_str = join ' ', @tokens;
I would like to rejoin the line with the original space. Is there a way that I can save a space from split and reuse it later? Or will it be a huge pain? It is mostly cosmetic, but I would like to keep the original spaces from the input string.
If you separate using a regular expression with brackets in parentheses, the separation pattern will be included in the resulting list (see perldoc -f split )
my @list = split /(\s+)/, 'a b c d'; print Data::Dumper::Dumper(\@list); VAR1 = [ 'a', ' ', 'b', ' ', 'c', ' ', 'd' ];
:
split /\b/, $line;
('a',' ','b',' ','c',' ','d')
: , \b , , . , Ether:
\b
split /(?:(?<=\S)(?=\s)|(?<=\s)(?=\S))/, $line;
Why don't you just do my $new_str = uc( $line );:?
my $new_str = uc( $line );
UPDATE - The original uc () is just a shorthand for a "more complex function".
Well, as a rule, you can also:
$line =~ s/(\S+)/more_complex_function($1)/ge;
Source: https://habr.com/ru/post/1725587/More articles:Post to a fan page of a Facebook fan with a backend? - apiStatic methods in C #? - performanceN-shaped intersection of sorted enumerations - c #Can I temporarily store data in my C # .Net application to reduce the need for data calls from SQL Server? - c ##define expression explained - cget a list of people using my facebook app - facebookDesign Pattern Help - pythonDifficulties using iterator and generics in implementing binary search - javagit svn fetch fatal: write error: Недопустимый аргумент - gitPHP [OOP]: allocating memory for inheritance - memory-managementAll Articles