tags:

views:

730

answers:

4

I thought this would have done it...

$rowfetch = $DBS->{Row}->GetCharValue("meetdays");
$rowfetch = /[-]/gi;
printline($rowfetch);

But it seems that I'm missing a small yet critical piece of the regex syntax.

$rowfetch is always something along the lines of:

------S
-M-W---
--T-TF-

etc... to represent the days of the week a meeting happens

+11  A: 
$rowfetch =~ s/-//gi

That's what you need for your second line there. You're just finding stuff, not actually changing it without the "s" prefix.

You also need to use the regex operator "=~" for this.

Thanks... thats what I thought... Just wasnt sure how to reassign it... Any way to do it in one line?
CheeseConQueso
Easier said than done regarding "one line"... I've never found a "pure" way of running a regular expression replace.
Use the 's' option for multiple lines s/-//gis
codelogic
So this regular expression says 'store into $rowfetch the characters that are not matched by the regular expression "s/-//gi" ' ?
CheeseConQueso
Not quite.The regular expression says "replace $rowfetch with all occurrences of the first set "-" replaced with the second set ""... thus deleting the hyphen."
@codelogic: the 's' modifier is irrelevant here. It lets the '.' (dot) match every character including line separators (\n, \r, etc.), but there are no dots in this regex.
Alan Moore
@cmartin - makes more sense.. thanks... that actually helped me put a large part of regex into perspective - there are two groups /(1)/(2)/ .. s means search expression set (1) and convert to expression set (2)?
CheeseConQueso
+6  A: 

Here is what your code presently does:

# Assign 'rowfetch' to the value fetched from:
#      The function 'GetCharValue' which is a method of: 
#         An Value in A Hash Identified by the key "Row" in:
#          Either a Hash-Ref or a Blessed Hash-Ref
#      Where 'GetCharValue' is given the parameter "meetdays"
$rowfetch = $DBS->{Row}->GetCharValue("meetdays");
# Assign $rowfetch to the number of times 
#  the default variable ( $_ ) matched the expression /[-]/ 
$rowfetch = /[-]/gi;
#  Print the number of times. 
printline($rowfetch);

Which is equivalent to having written the following code:

$rowfetch = ( $_ =~ /[-]/ ) 
printline( $rowfetch );

The magic you are looking for is the

=~

Token instead of

=

The former is a Regex operator, and the latter is an assignment operator.

There are many different regex operators too:

if( $subject =~ m/expression/  ){
}

Will make the given codeblock execute only if $subject matches the given expression, and

$subject =~ s/foo/bar/gi

Replaces ( s/) all instances of "foo" with "bar", case-insentitively (/i), and repeating the replacement more than once(/g), on the variable $subject.

Kent Fredric
+4  A: 

Using the tr operator is faster than using a s/// regex substitution.

$rowfetch =~ tr/-//d;

Benchmark:

use Benchmark qw(cmpthese);

my $s = 'foo-bar-baz-blee-goo-glab-blech';

cmpthese(-5, {
  trd => sub { (my $a = $s) =~ tr/-//d },
  sub => sub { (my $a = $s) =~ s/-//g },
});

Results on my system:

         Rate  sub  trd
sub  300754/s   -- -79%
trd 1429005/s 375%   --
+1  A: 

Off-topic, but without the hyphens, how will you know whether a "T" is Tuesday or Thursday?

AmbroseChapel
Thursday is R.. I just put that data up there so people would know what I was talking about in a general sense.
CheeseConQueso