Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: Building/de...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 10977 of 11532
Post > Topic >>

Re: Building/defining variables from an value in string

by Uri Guttman <uri@[EMAIL PROTECTED] > Apr 23, 2008 at 08:46 AM

>>>>> "AG" == Aruna Goke <myklass@[EMAIL PROTECTED]
> writes:

  AG> my $string =
"field1,int,10#field2,string,abc#field3,varchar,abc123";
  AG> my @[EMAIL PROTECTED]
 = split(/#/,$string);

  AG> my %h;
  AG> my @[EMAIL PROTECTED]
  AG> for (@[EMAIL PROTECTED]
){
  AG> @[EMAIL PROTECTED]
 = (split/,/)[0,2];
  AG> %h = (@[EMAIL PROTECTED]
);

simple one liner (untested) (assumes clean data with no # or , in it):

my %hash = $string =~ /(\w+),\w+,(\w+)/g ;

using a regex to parse out all the key/value pairs is simpler and much
much faster than all those splits and loops. and only 1 (the final)
variable needs to be declared.

uri

-- 
Uri Guttman  ------  uri@[EMAIL PROTECTED]
  --------  http://www.sysarch.com
--
-----  Perl Code Review , Architecture, Development, Training, Sup****t
------
--------- Free Perl Training --- http://perlhunter.com/college.html
---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com
---------
 




 5 Posts in Topic:
Building/defining variables from an value in string
rmalghan@[EMAIL PROTECTED  2008-04-22 14:08:33 
Re: Building/defining variables from an value in string
jialinli1981@[EMAIL PROTE  2008-04-22 16:52:56 
Re: Building/defining variables from an value in string
myklass@[EMAIL PROTECTED]  2008-04-23 07:38:46 
Re: Building/defining variables from an value in string
noreply@[EMAIL PROTECTED]  2008-04-23 09:19:48 
Re: Building/defining variables from an value in string
Uri Guttman <uri@[EMAI  2008-04-23 08:46:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 6:15:20 CDT 2008.