Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Using the Variable Name from an Array

by perlgates (Initiate)
on Nov 14, 2019 at 15:55 UTC ( [id://11108676]=perlquestion: print w/replies, xml ) Need Help??

perlgates has asked for the wisdom of the Perl Monks concerning the following question:

I want to get at the variable name from the @ element array and assign it to $DB_Column Name>

@element= ( $id,$Record_Lock,$Record_Change_Date,$Purchase_PO,$Project_Group, $Project_Name,$Project_Active,$Project_Vendor,$Project_Prime,$Projec +t_Status, $Site_ID,$Site_Name,$Site_Division,$Site_Country,$Site_AP_Count, $Survey_Data_Date,$Survey_First_Date,$Survey_Last_Date,$Survey_Count +, $Survey_Status,$Survey_Status_Manual,$Survey_GT_Three,$Map_Approved_ +Date,$Map_Architecht, $Cutover_Approved_Date,$Survey_Approved_Date,$Map_Billed_Month,$Cuto +ver_Billed_Month,$Survey_Billed_Month) foreach $Field_Name(@element) { $X1=$X."px"; $Y=$Y."px"; $Z1=$Z."px"; print <<ENDHTML; <div id="contentframe" style="position:absolute; top: $X1; Left: $Y1;" +>$DB_Column_Name</div> <div id="contentframe" style="position:absolute; top: $X1; Left: $Z1;" +> <input type="text" name="$DB_Column_Name" style="width: 120px;"va +lue="$Field_Name"> <br></div> ENDHTML $X=$X+30; }

Replies are listed 'Best First'.
Re: Using the Variable Name from an Array
by davido (Cardinal) on Nov 14, 2019 at 17:48 UTC

    Learn about proper references (perlref, perlreftut, perldsc, perllol), and hashes (perldata, perlintro). You're asking about how to do a thing because you are unaware of a better thing to do. Symbolic references are possible (and the reading I linked to will even show you how), but there are vastly better ways to do what you want using hashes, and first-class references.


    Dave

Re: Using the Variable Name from an Array
by hippo (Bishop) on Nov 14, 2019 at 16:15 UTC

    It's not 100% clear to me what you want to do or expect to happen here, but perhaps an AoH would be better suited than a plain array?

    #!/usr/bin/env perl use strict; use warnings; my $id = 'foo'; my $Record_Lock = 'bar'; my $Record_Change_Date = 'baz'; my @elements = ( {name => 'id', value => $id }, {name => 'Record_Lock', value => $Record_Lock }, {name => 'Record_Change_Date', value => $Record_Change_Date} ); for my $elem (@elements) { print "Elem name $elem->{name} has value $elem->{value}\n"; }
      Simplified question what if I want to do this print the array element varibale name and value
      Example:
      $test=12; $test=13; @array=($test,$test2); foreach $value(@array) { print ?Variable_Name? $value; }

      Getting @ the variable_name inside of the array is what I can not remember how to do.
        Getting @ the variable_name inside of the array is what I can not remember how to do.

        You don't have a variable_name inside of the array available by assignment. You have the values of the variables.

        If you want to access the values by name, do so using a hash:

        $test =12; $test2 =13; %hash=( test => $test, test2 => $test2, ); foreach $name(qw(test test2)) { print "$name $hash{$name}\n"; }
        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        The array stores the values, not the variables and their names.
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Using the Variable Name from an Array
by BillKSmith (Monsignor) on Nov 14, 2019 at 16:54 UTC
    I think you are asking how to get strings (such as 'ID', 'Record_Lock', etc) from the array @elements. This is not possible. Any ARRAY contains only a LIST of SCALARS. The name that you use in your source code is not part of the scalar hence it not part of the array. It is possible to create a scalar which contains the name of a variable. This is called a 'symbolic reference'. It is almost always a bad idea (not even allowed under 'use strict'). You need a more appropriate data structure (probably a hash or an array of hashes). We can help you design this structure if you provide us with a small sample of your input data and the expected output from that sample.
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11108676]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found