🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.perl
Article #51765

Re: Pisanie po stringu w określonym miejscu

#51765
From: Szymon Sokół
Date: Wed, 13 Jan 2010 19:59
39 lines
2581 bytes
On Wed, 13 Jan 2010 11:37:47 -0800 (PST), Mikołaj Karwat wrote:

> Witam Wszystkich,
> 
> Jak w perlu zmodyfikować string na określonej pozycji?

Albo używasz odpowiedniego regexpa, np. tak:

my $str = "ra";
my $str_len = length($str);
my $some_str = "Mikołaj Karwat";
my $some_pos = index($some_str, "ar");
$some_str =~ s/(.{$some_pos}).{$str_len}/$1$str/;

albo - zazwyczaj tak będzie szybciej i prościej - funkcji substr:

my $str = "ra";
my $some_str = "Mikołaj Karwat";
my $some_pos = index($some_str, "ar");
substr($some_str, index($some_str, "ar"), length($str), $str);

Warto jednak zauważyć, że jeśli chcesz zamienić np. "ar" na "ra", tak jak w
tym przypadku, to zamiast szukać miejsca, gdzie zastępowany string występuje
i następnie podmieniać substring, lepiej zrobić to bezpośrednio:

my $str = "ra";
my $some_str = "Mikołaj Karwat";
$some_str =~ s/ar/ra/;

Perl ma po prostu dość znacząco odmienną filozofię niż C, zwłaszcza w
zakresie przetwarzania napisów i tablic, więc przepisywanie żywcem w Perlu
algorytmu zapisanego w C owocuje zazwyczaj programem nieczytelnym i
nieefektywnym.

-- 
Szymon Sokół (SS316-RIPE) -- Network Manager                                 B
Computer Center, AGH - University of Science and Technology, Cracow, Poland  O
http://home.agh.edu.pl/szymon/ PGP key id: RSA: 0x2ABE016B, DSS: 0xF9289982  F
Free speech includes the right not to listen, if not interested -- Heinlein  H
Technical Details
Message-ID: <1kc5y96ntukix$.dlg@falcon.sloth.hell.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!dustpuppy.sloth.hell.pl!not-for-mail
References: <5cfd48ff-7e13-4683-8ae3-43927c6c3e59@s31g2000yqs.googlegroups.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!dustpuppy.sloth.hell.pl!not-for-mail
From: Szymon Sokół
 <szymon@bastard.operator.from.hell.pl>
Newsgroups: pl.comp.lang.perl
Subject: Re: Pisanie po stringu w określonym miejscu
Date: Wed, 13 Jan 2010 20:59:49 +0100
Organization: Guy Fawkes Society
Lines: 39
Sender: szymon@news.agh.edu.pl
Message-ID: <1kc5y96ntukix$.dlg@falcon.sloth.hell.pl>
References: <5cfd48ff-7e13-4683-8ae3-43927c6c3e59@s31g2000yqs.googlegroups.com>
NNTP-Posting-Host: chello089079192166.chello.pl
Mime-Version: 1.0
X-Trace: news.agh.edu.pl 1263413069 1398 89.79.192.166 (13 Jan 2010 20:04:29 GMT)
X-Complaints-To: abuse@agh.edu.pl
NNTP-Posting-Date: Wed, 13 Jan 2010 20:04:29 +0000 (UTC)
User-Agent: 40tude_Dialog/2.0.15.1 Hamster/2.1.0.11
X-Face: xic?6f"rD)k-tM.]L]A6l4W7_aGt({}&{D,hiRI.8cPtf,1F.w@Qkl!vr23o1UY-BfXO$p< hk5>ijB?eBy)?!C<@)+%U\&`1X)MJ>.i.qgkY]4mvL62j<|xJRG/5:7:l8q]x;v!)Bz^r/!yG8DxoD -@fnC,g]BAXj
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit