School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,480 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,765 people online right now. Registration is fast and FREE... Join Now!




pattern matching!

 

pattern matching!

Sun751

8 Jun, 2009 - 04:44 AM
Post #1

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
CODE

use strict;
use warnings;


my $number = 5011;
$number =~ /(\d)(\d)(\d)(\d)/;
if ($1)
{
    print qw(this is 1st digit!);
}
elsif ($2)
{
    print qw(this is 2nd digit!);
}
elsif ($3)
{
    print qw(This is 3rd digit!);
}


In above code i am expecting it to print 1st digit, 2nd digit and 3rd digit, but its not working that way
its just printing 1st digit. Can any one tell me whats wrong????

User is offlineProfile CardPM
+Quote Post


n00bc0der

RE: Pattern Matching!

8 Jun, 2009 - 05:28 AM
Post #2

D.I.C Head
**

Joined: 27 Nov, 2008
Posts: 62



Thanked: 3 times
My Contributions
While with if's and elsif's as soon as one validates to true so:

if (this is true) {

the program enters here
}

elsif {

this section will never get entered
}

Your algorithm is wrong here, you could do a sequences of if's, but i think you should redesign your algorithm here.

User is offlineProfile CardPM
+Quote Post

KevinADC

RE: Pattern Matching!

8 Jun, 2009 - 07:49 AM
Post #3

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
And the print lines are going to look funny because you are using 'qw' which makes a list, then you print the list so all the words in the list will just run together. You should use 'qq' for strings with variable interpolation and 'q' for string without variable interpolation.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:46AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month