2

How to convert string to variable (string variable --> $variable)?

Or a list of variables delimited by commas then convert to actual variables.

I have 2 files:

  • column names file
  • rows file

I need to match a whole row from the rows file based on a string, and name each row based from the column names file.

Example:

COLUMN NAMES FILE content

name,age,gender

ROWS FILE content

mar,44,m
rich,49,m
glenn,22,m

Now I have a variable string $who = 'glenn'

I will first match it on the ROWS FILE.

Then I need to name its matched contents with an actual variable with its name based on the COLUMN NAMES FILE.

Output:

$name = 'glenn';
$age = 22;
$gender = 'm';

I need something that dynamically changes, creating the needed variables and contents automatically by only updating the content files.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • 3
    Okay, so if you achieve this, how will you know what variable names to use in the rest of your Perl code? – Borodin Jul 26 '16 at 11:08
  • 1
    @Borodin , I am building a universal/standard module at the company where I am working (called using require). Since this will be a module, there will expectations like what are the available variable. And for me not to worry, I will follow the SOLID principle, I can only add but not modify the expected available variables. Thanks for asking. Let me know if I can answer any other questions... – Digimon Digitizer 2 Jul 26 '16 at 11:16
  • 5
    It's still a terrible idea to crap all over your namespace like this. What if someone creates a column called `/`? Or `0`? Or `^I`? You'll create some HORRIFIC bugs that will be an absolute horror to trace. – Sobrique Jul 26 '16 at 11:24
  • 1
    @Sobrique don't worry, "IT WON'T HAPPEN". Because the column names I am planning to create are based from a ora sql table, which is a production table. – Digimon Digitizer 2 Jul 26 '16 at 11:26

1 Answers1

9

What you're asking for, is not what you want. This is a bad idea for many many reasons, but they're outlined here: http://perl.plover.com/varvarname.html

What you need for this is a hash, and specifically a slice.

It works a bit like this:

my @header = qw ( A B C );
my @values = qw ( X Y Z );
my %combined;
@combined{@header} = @values; 
print Dumper \%combined; 

Gives:

$VAR1 = {
          'C' => 'Z',
          'A' => 'X',
          'B' => 'Y'
        };

(NB: Hashes are explicitly UNORDERED, so you get a random sequence each time you print. That's by design, because they're supposed to be accessed via keys anyway)

So with your data:

#!/usr/bin/env perl
use strict;
use warnings 'all'; 

open ( my $cols, '<', 'cols_file.txt') or die $!;
chomp ( my @cols = split /,/, <$cols> ); #just reads first line. 
close ( $cols );
open ( my $rows, '<', 'rows_file.txt' ) or die $!; 
while ( <$rows> ) {
    chomp;
    my %this_row;
    @this_row{@cols} = split /,/;
    push ( @all_rows, \%this_row );
}
close ( $rows );

print Dumper \@all_rows; 

You can then access individual elements from rows e.g.:

foreach my $row ( @all_rows ) {
    print $row -> {name},"\n";
 }
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • Thank you very very very very MUCH! You made my day! It worked like charm! :D I am so glad someone like you answered my question! Thank you so much once again. I have achieved what I need! :) – Digimon Digitizer 2 Jul 26 '16 at 12:01