Hi Shaji, On Thu, 15 Aug 2013 00:26:55 +0800 (SGT) *Shaji Kalidasan* <shajiin...@yahoo.com> wrote:
> Greetings, > > I am trying to get name, phone and address in a lexical variable and trying > to print it, but, I am not getting the expected output. Any help is greatly > appreciated. > > [code] > use strict; > use warnings; > > while (<DATA>) { > my ($name, $phone, $address) = /^([a-zA-Z ]+):([\-\d]+):([\w, ])$/; This regex requires two colon characters (":") but you only have one in every line of the example data. So it doesn't match and it returns an empty list. You can and should test if a regex match like this: if (my ($name, $phone, $address) = ($string =~ /…/)) { # Success! } else { die "The string does not match."; # Or some other error handling. } Regards, Shlomi Fish > print "Name : $name\n" if defined $name; > print "Phone : $phone\n" if defined $phone; > print "Address : $address\n" if defined $address; > } > > __DATA__ > Sachin Tendulkar:408-724–0140:23, Brooke Street, Sunnyvale,CA 94088 > Mahendra Singh Dhoni:408–456–1234:76, Charles Street, Washington, MA 02131 > Rahul Dravid:408–253–3122:123, Queens Street, Canberra, Australia > Virendar Sehwag:293–259–5395:15 Hilton Avenue, Sydney, Australia > [/code] > > > Where am I going wrong?. > > best, > Shaji > ------------------------------------------------------------------------------- > Your talent is God's gift to you. What you do with it is your gift back to > God. > ------------------------------------------------------------------------------- -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ "Star Trek: We, the Living Dead" - http://shlom.in/st-wtld Larry Wall can program in his sleep. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/