Gossamer Forum
Home : General : Perl Programming :

Developers block <g>

(Page 1 of 2)
> >
Quote Reply
Developers block <g>
Hi

Damn, I've got the developer's block. I know the following line is completely wrong but I can't get my head around how to write it properly. Can anyone help?

Code:
$rd_bt_key_no_no = $rd_bt_key = /[0-9]//g;

What I'm trying to do is to assign the variable "$rd_bt_key_no_no" with the variable "$rd_bt_key" but without any numbers in it, therefore the substitution "/[0-9]//g;".

Wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
Damn you always use such complicated variable names :)

$rd_bt_key_no_no = $rd_bt_key =~ s/\d//g;

Last edited by:

PaulW: Nov 29, 2001, 6:27 AM
Quote Reply
Re: [PaulW] Developers block <g> In reply to
I know. I often confuse myself. Thanks for that, however it's inside a hash loop so I'm getting...

Modification of a read-only value attempted at /home/fba/cgi.fbagroup.co.uk/opticdb/geo-drws.co.uk/odb_db.pl line 305..

I guess I'll need to assign the variable to another name before modifying??

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
OK. Got it.

Code:
$rd_bt_key_no_no = $rd_bt_key;
$rd_bt_key_no_no =~ s/\d+//g;

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
>>$rd_bt_key_no_no =~ s/\d+//g;<<

As you are using g then you don't need the +
Quote Reply
Re: [PaulW] Developers block <g> In reply to
g is for global isn't it? + just makes it more efficent.

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
They're completly different things aren't they ?

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
Yes g is global


Last edited by:

PaulW: Nov 29, 2001, 8:29 AM
Quote Reply
Re: [Wil] Developers block <g> In reply to
Yes they are different but + is irrelevant when using g.

g is gonna wipe out all the digits so there is no point using + which matches 1 or more digits.
Quote Reply
Re: [PaulW] Developers block <g> In reply to
Putting the perl line into English it would read:

Look for any digit that appear 1 or more times, substitute this with a blank space, and look for this pattern globally throughout $_.

So really you're applying the block, globally. In this case there's no difference, but this is really a rare case.

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
>>Putting the perl line into English it would read:

Look for any digit that appear 1 or more times, substitute this with a blank space, and look for this pattern globally throughout $_.
<<

No it wouldn't.....it isn't substituting with a space nor using $_

>>In this case there's no difference, but this is really a rare case. <<

Actually it's a pretty standard case. using s/\d//g; and s/\d+//g; is exactly the same. The only time it makes a difference is when you want to globally substitute a certain pattern, eg.......

If you had a string like .... A1AB5DSH55DHS67

Using s/\d{2,}//g; would leave A1AB5DSHDHS


Last edited by:

PaulW: Nov 29, 2001, 8:57 AM
Quote Reply
Re: [PaulW] Developers block <g> In reply to
No they're aren't the same. Using \d+ is faster in most cases. Look at the following example:

Code:
$x = "12345123451234512345x456456456456y8989";
$x =~ s/\d+//g; # faster than s/\d//g

Can you see why?

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
If I coded more strictly I should also be using tr///; here instead of s///;.

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
The difference is negligible.

Substituting a few digits just using g and using \d+ will make absolutely no noticable difference.

Coding strictly doesn't mean you should use tr///; at all.
Quote Reply
Re: [PaulW] Developers block <g> In reply to
No but it makes more logical sense to use tr///; in my case.

I wasn't comparing \g and \d+ at all. Those are entierly different things and shouldn't even be compared.

What I was asking is can you see the difference between using \d and \d+.

$x = "12345123451234512345x456456456456y8989";
$x =~ s/\d+//g; # faster than s/\d//g


Can you see what I'm trying to get at here?

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
tr is for basic substitutions.

>>
What I was asking is can you see the difference between using \d and \d+.
Can you see what I'm trying to get at here?
<<

Yeah I know what you were asking, and I know what the difference between \d and \d+ is Laugh.....and I was telling you that there was no difference in relation to what you were trying to achieve.

Last edited by:

PaulW: Nov 29, 2001, 9:17 AM
Quote Reply
Re: [Wil] Developers block <g> In reply to
Argh.. can't remember where the brackets are supposed to go... gotta go.

Last edited by:

brewt: Nov 29, 2001, 9:58 AM
Quote Reply
Re: [brewt] Developers block <g> In reply to
Are you alright?!

- wil
Quote Reply
Re: [PaulW] Developers block <g> In reply to
Don't really agree with you there. I am actually translating my string from fo01 to foo. The digits in my string are being translated into nothing in my case. But of course, TMTOWTDI.

There is a difference, that's why I posted that example. In the code I am writing - actually using \d+ is faster than using \d.

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
>>I am actually translating my string from fo01 to foo<<

So then you'd use:

s,\d{2}$,o,;

>>In the code I am writing - actually using \d+ is faster than using \d.
<<

Prove it.
Quote Reply
Re: [PaulW] Developers block <g> In reply to
No. I would be using tr/[1-9]//; Because that's the way I'm translating my string.

I can prove it and benchmark it for if you want but can't you see the logic behind my statement??

- wil
Quote Reply
Re: [Wil] Developers block <g> In reply to
In Reply To:
Are you alright?!
hehe, I had a bus to catch, that's all.

Code:
($rd_bt_key_no_no = $rd_bt_key) =~ s/\d+//g;
(fixed)
should work as a one liner Smile That's what I meant by I can't remember where the brackets were supposed to go.



Adrian

Last edited by:

brewt: Nov 29, 2001, 2:24 PM
Quote Reply
Re: [brewt] Developers block <g> In reply to
>>should work as a one liner<<

not until you add an s :)
Quote Reply
Re: [PaulW] Developers block <g> In reply to
well yeah... i just blindly copied his code above and added the brackets Tongue


Adrian
Quote Reply
Re: [brewt] Developers block <g> In reply to
>>i just blindly copied his code <<

Did Jason have his hands over your eyes Angelic
> >