Building/defining variables from an value in string

Building/defining variables from an value in string

am 22.04.2008 23:08:33 von Ravi Malghan

Hi: I have a string with a number of variable name, type and value pairs. I want to split the field and build my variables. Below is an example

$string = "field1,int,10#field2,string,abc";
@values = split(/#/,$string);

I want to get two variables from the string, equivalent to the below statements
$field1=10;
$field2="abc";

TIA.
Ravi



____________________________________________________________ ________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Building/defining variables from an value in string

am 22.04.2008 23:52:56 von jialin

use strict;
my $string = 'field1,int,10#field2,string,abc';
my @values = split /#/,$string;


my @vars;
no strict 'refs';
for (@values) {
my @tmp = split /,/;
push @vars, $tmp[0];
${$tmp[0]} = $tmp[2];
}

for (@vars) {
print $_, ": ", ${$_}, "\n";
}
__END__


this will solve your problem, but it uses symbolic reference whose
use, I think, is discouraged.

A better solution is to use hash:

use strict;
my $string = 'field1,int,10#field2,string,abc';
my @values = split /#/,$string;

my %vars;
for (@values) {
my @tmp = split /,/;
$vars{$tmp[0]} = $tmp[2];
}

use Data::Dumper;
print Dumper \%vars;

__END__

On Tue, Apr 22, 2008 at 4:08 PM, Ravi Malghan wrote:
> Hi: I have a string with a number of variable name, type and value pairs. I want to split the field and build my variables. Below is an example
>
> $string = "field1,int,10#field2,string,abc";
> @values = split(/#/,$string);
>
> I want to get two variables from the string, equivalent to the below statements
> $field1=10;
> $field2="abc";
>
> TIA.
> Ravi
>
>
>
> ____________________________________________________________ ________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Building/defining variables from an value in string

am 23.04.2008 08:38:46 von Goke Aruna

Li, Jialin wrote:
> use strict;
> my $string = 'field1,int,10#field2,string,abc';
> my @values = split /#/,$string;
>
>
> my @vars;
> no strict 'refs';
> for (@values) {
> my @tmp = split /,/;
> push @vars, $tmp[0];
> ${$tmp[0]} = $tmp[2];
> }
>
> for (@vars) {
> print $_, ": ", ${$_}, "\n";
> }
> __END__
>
>
> this will solve your problem, but it uses symbolic reference whose
> use, I think, is discouraged.
>
> A better solution is to use hash:
>
> use strict;
> my $string = 'field1,int,10#field2,string,abc';
> my @values = split /#/,$string;
>
> my %vars;
> for (@values) {
> my @tmp = split /,/;
> $vars{$tmp[0]} = $tmp[2];
> }
>
> use Data::Dumper;
> print Dumper \%vars;
>
> __END__
>
> On Tue, Apr 22, 2008 at 4:08 PM, Ravi Malghan wrote:
>> Hi: I have a string with a number of variable name, type and value pairs. I want to split the field and build my variables. Below is an example
>>
>> $string = "field1,int,10#field2,string,abc";
>> @values = split(/#/,$string);
>>
>> I want to get two variables from the string, equivalent to the below statements
>> $field1=10;
>> $field2="abc";
>>
>> TIA.
>> Ravi
>>
>>
>>
>> ____________________________________________________________ ________________________
>> Be a better friend, newshound, and
>> know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>> For additional commands, e-mail: beginners-help@perl.org
>> http://learn.perl.org/
>>
>>
>>
>

my $string = "field1,int,10#field2,string,abc#field3,varchar,abc123";
my @vals = split(/#/,$string);

my %h;
my @arr;

for (@vals){
@arr = (split/,/)[0,2];
%h = (@arr);
print keys %h; print "---"; say values %h;
}


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Building/defining variables from an value in string

am 23.04.2008 09:19:48 von Gunnar Hjalmarsson

Aruna Goke wrote:
>
> print keys %h; print "---"; say values %h;

say keys %h, '---', values %h;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/