On Thu, Jan 02, 2003 at 08:05:46PM +0100, Martin Baehr wrote:
here is the first example though:
second: not sure if pasting from hilfe is a good idea. probably not, because, then you can't cut the examples and paste them for your own use...
i was however quite excited to see a few examples that look a lot nicer and compact in pike compared to perl :-)
Accessing Substrings //------------------------------ string str, value; int offset, count; value = str[offset..offset+count]; value = str[offset..];
string newstring, newtail; str = str[..offset-1]+newstring+str[offset+count..]; str = str[..offset-1]+newtail;
//------------------------------ // get a 5-byte string, skip 3, then grab 2 8-byte strings, then the rest string leading, s1, s2, trailing; [leading, s1, s2, trailing] = array_sscanf(str, "%5s%*3s%8s%8s%s");
// split at five byte boundaries array fivers = array_sscanf(str, "%{%5s%}");
// chop string into individual characters array chars = str/"";
//------------------------------ // switching to hilfe: $ pike Pike v7.2 release 340 running Hilfe v2.0 (Incremental Pike Frontend)
str = "This is what you have";
Result: "This is what you have"
string first, start, rest, last, end, piece; int t = str[0];
Result: 84
first = str[0..0];
Result: "T"
start = str[5..5+1];
Result: "is"
rest = str[13..];
Result: "you have"
last = str[sizeof(str)-1..sizeof(str)-1];
Result: "e"
end = str[sizeof(str)-4..];
Result: "have"
piece = str[sizeof(str)-8..sizeof(str)-8+2];
Result: "you"
str = "This is what you have";
Result: "This is what you have"
str = replace(str, ([ " is ":" wasn't " ]) );
Result: "This wasn't what you have"
str = str[..sizeof(str)-13]+"ondrous";
Result: "This wasn't wondrous"
str = str[1..];
Result: "his wasn't wondrous"
str = str[..sizeof(str)-11];
Result: "his wasn'"
str = "This is what you have";
Result: "This is what you have"
str = replace(str[..4], ([ "is":"at" ]) )+str[5..];
Result: "That is what you have"
str = "make a hat";
Result: "make a hat"
[str[0], str[-1]] = ({ str[-1], str[0] }); str;
Result: "take a ham"
string a, b, c; a="To be or not to be";
Result: "To be or not to be"
b=a[6..11];
Result: "or not"
b=a[6..7]; c=a[3..4];
Result: "or" Result: "be"
write("%s\n%s\n", b, c);
or be Result: 6
//------------------------------------------------
string cut2fmt(int ... positions)
{ string template = ""; int lastpos = 1; foreach(positions, int place) { template += "A" + (place - lastpos) + " "; lastpos=place; } template += "A*"; return template; }
string fmt=cut2fmt(8, 14, 20, 26, 30);
Result: "A7 A6 A6 A6 A4 A*"
write(fmt+"\n");
A7 A6 A6 A6 A4 A* Result: 18