如何将原始字符串转换为变量(变量-> $ variable)? [英] How to convert raw string to variable (variable --> $variable)?

查看:191
本文介绍了如何将原始字符串转换为变量(变量-> $ variable)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串转换为变量(字符串变量-> $ variable)?

或者用逗号分隔的变量列表,然后转换为实际变量.

我有2个文件:

  • 列名称文件
  • 行文件

我需要基于一个字符串来匹配行文件中的整行,并根据列名称文件来命名每一行.

示例:

列名文件内容

name,age,gender

行文件内容

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

现在我有一个可变字符串$who = 'glenn'

我将首先在 ROWS FILE 上将其匹配.

然后,我需要使用一个实际变量来命名其匹配内容,该变量的名称基于 COLUMN NAMES FILE .

输出:

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

我需要一些可以动态更改的东西,只需更新内容文件即可自动创建所需的变量和内容.

解决方案

您要的不是您想要的.出于很多原因,这是一个坏主意,但请在此处进行概述: http://perl.plover. com/varvarname.html

为此,您需要的是哈希,尤其是切片.

它的工作原理如下:

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

赠予:

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

(注意:哈希值是明确无序的,因此每次打印时都会得到一个随机序列.这是设计使然,因为无论如何都应该通过键来访问它们)

关于您的数据:

#!/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; 

然后您可以访问行中的各个元素,例如:

foreach my $row ( @all_rows ) {
    print $row -> {name},"\n";
 }

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.

解决方案

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";
 }

这篇关于如何将原始字符串转换为变量(变量-> $ variable)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆