Sunday, February 15, 2009

String split problem

스트링에 아래와 같은 문자열이 저장되어 있다고 할 때,

my $string=q/ man , girl, woman, boy, "man,woman", "girl,boy"/ ;

콤마(,) 를 기준으로 각 단어를 나눌려면 어떻게 해야할까?
이때, 따옴표(")로 묶여있는 단어 사이의 콤마는 무시 한다.

나누어
@result=( 'man', 'girl', 'woman', 'boy', 'man,woman', 'girl,boy' )
6개의 element로 저장하는 것이 최종 목표.

내 답은 아래와 같다. 더 깔끔하고, 간결한 코드가 있겠지?

my @result;
map { /"/?push @result,$_:push @result, split /,/,$_ } split /(".*?")/, $string;

Wednesday, February 4, 2009

Lookaround in PERL

'Lookaround' , a regular expression in perl, could be used as anchor in regular expression. The anchor which I mean here is a point in the string from which the perl scan for a given regular expression.

Examples of anchor in perl.



'great job boy'=~/^g/; # '^' is used to anchor
'great job boy'=~/y$/; # '$' is used to anchor


Alike the anchors above exam code, lookaround could be used similary as below



'great job boy'=~/(?=job).*boy/; # (?=pattern) is a form of positive lookaround
'great job boy'=~/(?!job).*boy/; # ( ?!pattern) is a form of negative lookaround


So how the above example lookaroud code works?

(?=job) anchors the position of match at 'j' of job.

In other words, perl scan the whole string 'great job boy' first, but after lookaround matches, perl start to search for remaining 'job boy'.

Then regular expression below matches or not?



'great job boy'=~/(?=job)boy/ ;


The answer is NO!!!

As I mentioned above, lookaround is working as anchor like '^'.
So (?=job) anchors at the position of j of 'job'. The first character
that could be matched after anchoring is j of 'job, not b of 'boy' which
is following job. Therefore, it doesn't match!

To make 'boy' match after anchoring (?=job), it need to be as below



'great job boy'=~/(?=job).*boy/;


What about negative lookaround? It's more complicated to understand.

Guess what happens below.



'YouAre'=~/(?!You)Are/;
Does it matches? It matches! Why it matches?
'You' comes before 'Are'. To regular expression success,
string before 'Are' should not be 'You'.

Then what on earth (?!You) anchor?
It should anchor where there's no 'You'.
Then it may be position 'o' or 'u' which does not match to 'You'.

Yes it is.
Then what is before 'Are' , which doesn't match to 'You'?
'u' can be! Therfore the above regular expression including negative lookaround success.


We can verify where negative lookaround anchors with some exams below


print "$`\t$'\n" if 'YouAre'=~/(?!You)/;
# Y ouAre

print "$`\t$'\n" if 'YouAre'=~/(?!You)e/;
# YouAr e

'YouAre'=~/You(?!Are)/;
# it doesn't match

Microsoft scripting game 2009

aero님의 블로그를 통해 지난해 microsoft에서 scripting game을
계최하고 있었다는 걸 알게 되었었다. 당시 이를 접한 시점에서 이미
Competetion이 끝나있어 아쉬워서, 다음 대회를 기다리고 있었는데
2009년에는 매년 2월 계최되던 winter game 에서 Summer game으로
변경되어 계최될 예정이라고 한다.

지난해 출제된 문제의 경우 굉장히 평이한 편이었는데, 올해는 좀 더 advance된
문제들이 출제될지 궁금하다. 올핸 꼭 참가하고 끝까지 문제를 풀어서
MS에서 기념품 한번 받아봐야겠다

Microsoft webpage for scripters: http://www.microsoft.com/technet/scriptcenter/default.mspx