how to interpret string as code?

J

Jason Quek

Hi there

As the fields in my data table are not fixed, I need to do this:

# ------------------------------------------------------------
$name = 'John Doe';
$fields = '$firstname, $lastname';
($fields) = split(/\s/, $name);
print "$firstname"; # returns John
print "$lastname"; # returns Doe
# ------------------------------------------------------------

where line 3 is interpreted as:
# ------------------------------------------------------------
($firstname, $lastname) = split(/\s/, $name);
# ------------------------------------------------------------

How can this be accomplished? Any help would be appreciated. Thank
you.



Jason Q.
 
A

Andrew McGregor

Jason said:
How can this be accomplished? Any help would be appreciated. Thank
you.

I'm not sure what the question is. What is line 2 attempting to do?
What happens when you replace line 3 with the last example of line 3?

#!/usr/bin/perl

use strict;
use warnings;

my $name = 'John Doe';

my ($firstname, $lastname) = split(/\s/, $name);

print "$firstname"; # returns John
print "$lastname"; # returns Doe
 
A

Anno Siegel

Jason Quek said:
Hi there

As the fields in my data table are not fixed, I need to do this:

# ------------------------------------------------------------
$name = 'John Doe';
$fields = '$firstname, $lastname';
($fields) = split(/\s/, $name);
print "$firstname"; # returns John
print "$lastname"; # returns Doe
# ------------------------------------------------------------

where line 3 is interpreted as:
# ------------------------------------------------------------
($firstname, $lastname) = split(/\s/, $name);
# ------------------------------------------------------------

How can this be accomplished? Any help would be appreciated. Thank
you.

Are you saying you want to determine the variable names ($firstname and
$lastname) at runtime? So that they could be $vorname and $nachname in
another run of the same program? You don't really want to do that.
How would you *use* those elusive variables in the rest of the program
if you don't know their names?

Instead, use a hash (untested):

my $name = 'John Doe';
my @fields = qw( firstname lastname);
my %person;
@person{ @fields} = split ' ', $name;
print "$person{ firstname}\n";
print "$person{ lastname}\n";

If in another run you set @fields = qw( vorname nachname) you can access
the parts under $person{ vorname} etc. But now the variable bits are
program data, not parts of the program proper. That is a much better
design.

Annp
 
J

Jason Quek

Andrew McGregor said:
I'm not sure what the question is. What is line 2 attempting to do?
What happens when you replace line 3 with the last example of line 3?
my ($firstname, $lastname) = split(/\s/, $name);

This works but the problem is the field order of my data is not fixed.
Sometimes it is ($firstname, $lastname), sometimes it is ($lastname,
$firstname) so I need specify the order of the fields only during
script execution.



Jason Q.

-----------------------------------------
 
J

Jason Quek

Are you saying you want to determine the variable names ($firstname and
$lastname) at runtime? So that they could be $vorname and $nachname in
another run of the same program? You don't really want to do that.
How would you *use* those elusive variables in the rest of the program
if you don't know their names?

Instead, use a hash (untested):

my $name = 'John Doe';
my @fields = qw( firstname lastname);
my %person;
@person{ @fields} = split ' ', $name;
print "$person{ firstname}\n";
print "$person{ lastname}\n";

If in another run you set @fields = qw( vorname nachname) you can access
the parts under $person{ vorname} etc. But now the variable bits are
program data, not parts of the program proper. That is a much better
design.

Annp

Yes you're right, the variable names are only avialable at runtime. I
understand your method and have several other work arounds, but I was
wondering if my 'method' were possible. Possibly using 'eval',
although I wasn't able to get it to work either.



Jason Q.
 
N

nobull

Jason said:
Subject: how to interpret string as code?

perldoc -f eval

But your question is really closer to "FAQ: How can I use a variable
as a variable name?".

See FAQ.
 
A

Anno Siegel

Jason Quek said:
Yes you're right, the variable names are only avialable at runtime. I
understand your method and have several other work arounds, but I was
wondering if my 'method' were possible. Possibly using 'eval',
although I wasn't able to get it to work either.

Not with lexicals. If you try declare them in eval() and use them
outside, that won't work because eval constitutes a lexical block.

For a "solution" with package variables, see the faq "How can I use
a variable as a variable name?" The faq also explains why this is a
bad idea.

Anno
 
B

Big and Blue

Jason said:
Yes you're right, the variable names are only avialable at runtime. I
understand your method and have several other work arounds, but I was
wondering if my 'method' were possible. Possibly using 'eval',
although I wasn't able to get it to work either.

As has been pointed out, the variable names you assign to need to be
known in advance (for use elsewhere in the program) - it's just the
assignment order you want to change.

Some simple options, by example:

They all assume that you can set $invert if you need to swap things.

1) Choose the l-value based on $invert

my ($f, $l);
$invert? ($l, $f): ($f, $l) = qw( first last );

print "f is $f\n";
print "l is $l\n";

2) Assign under one assumption the swap if necessary:

my ($f, $l) = qw( first last );
($f, $l) = ($l, $f) if ($invert);

print "f is $f\n";
print "l is $l\n";
 
J

Joe Smith

As the fields in my data table are not fixed, ...


In that case, use a variable set of array indexes to
access the appropriate columns.
# ------------------------------------------------------------
$name = 'John Doe';
$fields = '$firstname, $lastname';


$fn_index = 0;
$ln_index = 1;
($fields) = split(/\s/, $name);


Without using 'eval', that makes no sense, and eval is
not the appropriate way to do it.
print "$firstname"; # returns John
print "$lastname"; # returns Doe


@fields = split ' ',$name;
print "First name = $fields[$fn_index]\n";
print "Last name = $filds[$ln_index]\n";

Another way would be

($firstname,$lastname) = @fields[$fn_index,$ln_index];

-Joe
 
J

Jürgen Exner

Jason said:
This works but the problem is the field order of my data is not fixed.
Sometimes it is ($firstname, $lastname), sometimes it is ($lastname,
$firstname) so I need specify the order of the fields only during
script execution.

if ($normal_order) {
my ($firstname, $lastname) = split(/\s/, $name);
} else {
my ($lastname, $firstname) = split(/\s/, $name);
}

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top