News for hackers. Stuff that matters.
Re:Interviewing programmers: coding test example explained by sram on Thursday June 03, 2010 @10:52AM (#1401462)
I always end up using perl when I have to do string manipulations and have let my c skills suffer as a result. Sad. Using perl regexp kills brain cells.

#!/usr/bin/perl

$in = <STDIN>;

$remove = <STDIN>;

chomp ($remove);

chomp ($in);

$in =~ s/$remove//g;

Re:Interviewing programmers: coding test example explained by nevinera on Thursday June 03, 2010 @11:17AM (#1401530)
Bad boy!

    >hello|goodbye
    >| 
    >>Result is 'hello|goodbye'

    >That's a nice dog you have there.
    >.
    >>Result is ''

I usually handle this with:

    my $pattern = '\\'.substr($in,0,1);
    $in =~ s/$pattern//g;
but it still doesn't feel safe. String operations in perl usually do what you want, but be careful with them!
Re:Interviewing programmers: coding test example explained by jerf on Thursday June 03, 2010 @12:10PM (#1401699)

    $in =~ s/\Q$remove\E//g;
man perlre, search for \Q. When \Q and \E'ing a variable like that, a literal "\E" in the variable value can not escape from the outer \Q.
Re:Interviewing programmers: coding test example explained by shabble on Thursday June 03, 2010 @12:42PM (#1401779)

     perl -pe'BEGIN { $x = quotemeta(shift) } s/$x//g;'
should do what you want. `quotemeta' is the function that does the escaping for your properly.

I was hoping to be able to use tr///, since it seems like it was built for this, but it creates its conversion tables at compile time, so there's no chance to interpolate the argument without some nasty nasty eval'ing.

Re:Interviewing programmers: coding test example explained by nevinera on Thursday June 03, 2010 @01:15PM (#1401872)
Yeah, tr was my instinct as well.. Thanks for quotemeta, it helps with something I'm working on right now!
Re:Interviewing programmers: coding test example explained by smackfu on Thursday June 03, 2010 @12:59PM (#1401827)
No, you have to do it in c so that there are all sorts of obscure bugs.