I am trying to do something like the following

my @type1_cols = qw(col1 col2 col3);
my ($col1, $col2, $col3) = split (/:/,$string);

...

        foreach my $col (@type1_cols){
            $hash->{$col} = ${$col};   # <---- this line is the issue. how do i do ${$col}
        }

any help is much appreciated.

Recommended Answers

All 2 Replies

Have you tried $hash->{$col} = eval(qq(\$$col)); ?

Why not do the following?

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @type1_cols = qw(col1 col2 col3);
my $string = 'beer:beef:cabbage';
my %hash;
($hash{'col1'}, $hash{'col2'}, $hash{'col3'}) = split (/:/,$string);

print Dumper(\%hash);

That looks easier to understand, to me, than having to resort to eval() .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.