in reply to Re: compare lists and delete unwanted from file
in thread compare lists and delete unwanted from file

Thankyou for your help. I have been trying to get a handle on hash references so this will be good. However, I don't understand the code snippet below. Specifically, what is the "(?" and the //gw.

my $geneids_to_remove = {}; # create a hash reference $text =~ s/(\w+) (?{ # in regex code $geneids_to_remove->{$1} = 1; # store geneids in a hash }) //gx;

Can you also explain the part below in more detail. Specifically, what does the /x do?

my $rx_find_geneid = qr/^(\w+) (?{ $gene_id = $1; })/x;

Replies are listed 'Best First'.
Re^3: compare lists and delete unwanted from file
by muppetjones (Novice) on Mar 14, 2012 at 17:28 UTC

    Of course!

    The (?{ ... }) is how you insert code into a regex. Whenever you see (? ... ), it generally means you are doing something with whatever is found inside, but you don't want to capture it. For instance (?: ... ) works just like a capture group but without actually capturing.

    Whenever you insert code into a regex, it's always a good idea to add the /x modifier, which tells the regex compiler to ignore all whitespace. This means you'll either need to escape whitespace (i.e., '\ '), or (even better) just use \s.

    The /g modifier means to match globally. This does different things depending on whether you're using s/// or m//. The former does it all at once, but the latter simply continues matching wherever the previous match left off, meaning you need a loop to match everything.