$array.fieldname question

Z

ZZT

Hello friends of perl,

have a problem and hope you can help me :)

okay, I have an ini-file looks like this:

------------------------------------------------------------------
[def] # name of section is completly unknown
var1=value1
var2=value2

[ref]
var1=value3
var2=value4
---------------------------------------------------------------


#in result I create an array for the section names
@sections=("def","ref");
#while $sections[1] then eq to "ref" for instance

# following variables should then be defined
$def.var1=value1;
$def.var2=value2;
$ref.var1=value3;
$ref.var2=value4;

#then I'd like to apply several routines with the help of this variables
foreach (@sections)
{
function1 ($$_.var1); # -> function1($def.var1);
# ...
}

How can I do this?

Thanks for your answers
bye
 
B

Brian McCauley

ZZT said:
$def.var1=value1;
$def.var2=value2;
$ref.var1=value3;
$ref.var2=value4;

That is not Perl5 syntax. Please learn about Perl's data strucutres
and express code fragments in your posts in (strict) Perl5 syntax.

I think you've been told this before....
http://groups.google.com/[email protected]
http://groups.google.com/[email protected]

If you are asking questions about Perl6 then please say so.

I shall assume you are planning to use Perl5 and mean:

$def{var1}='value1';
$def{var2}='value2';
$ref{var1}='value3';
$ref{var2}='value4';
@sections=("def","ref");
foreach (@sections)
{
function1 ($$_.var1); # -> function1($def.var1);
}

How can I do this?

You are asking "How can I use a variable as a variable name?". This
is a FAQ.

Simply correct the syntax as described above and it will work so long
as %def and %ref are package variables.

But this is using symbolic references you should should not do that
unless you have a damn good reason (you don't).

You should use real references and a HoH.

For details see aforementioned FAQ.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

Jürgen Exner

ZZT said:
okay, I have an ini-file looks like this:

There are modules on CPAN to read INI files.
Did you check those?
#in result I create an array for the section names
@sections=("def","ref");
#while $sections[1] then eq to "ref" for instance
# following variables should then be defined
$def.var1=value1;
$def.var2=value2;
$ref.var1=value3;
$ref.var2=value4;

That is impossible because it is not Perl.
Perl does not use the dot notation. Maybe you are confusing Perl with Pascal
or Modula?
#then I'd like to apply several routines with the help of this
variables foreach (@sections)
{
function1 ($$_.var1); # -> function1($def.var1);

I smell symbolic references. Please check the FAQ (perldoc -q "variable
name") and Google about why this is a very bad idea.
How can I do this?

Instead of telling you how to shoot yourself in the foot I'd rather suggest
to use a better data structure like a hash (for each section in the INI
file) of hashes (for each definition within a section).

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top