Talk About Network



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 > [Fwd: Re: Build...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 10980 of 11060
Post > Topic >>

[Fwd: Re: Building/defining variables from an value in string]

by myklass@[EMAIL PROTECTED] (Aruna Goke) Apr 23, 2008 at 11:57 AM

-------- Original Message --------
Subject: Re: Building/defining variables from an value in string
Date: Wed, 23 Apr 2008 04:46:21 -0400
From: Uri Guttman <uri@[EMAIL PROTECTED]
>
To: myklass@[EMAIL PROTECTED]
 perl.beginners
References: <671418.60994.qm@[EMAIL PROTECTED]
> 
<f223dcde0804221452r61c40debg24b7471f327b1a65@[EMAIL PROTECTED]
> 
<480ED976.1020108@[EMAIL PROTECTED]
>

The following message is a courtesy copy of an article
that has been posted to perl.beginners as well.

>>>>> "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, Support 
------
--------- Free Perl Training --- http://perlhunter.com/college.html

---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com

---------


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

for (keys %hash){
say $_ ,' -- ', $hash{$_};
}

goksie




 1 Posts in Topic:
[Fwd: Re: Building/defining variables from an value in string]
myklass@[EMAIL PROTECTED]  2008-04-23 11:57:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Mon May 12 21:44:04 CDT 2008.