Get variable name from a list
Is this possible?
I am trying to populate a hash with keys as variable name and value as
variable value eg:
my %hash;
foreach my $w ($filename,$file_start,$file_time,$video_track,$audio_track, $quality,$sync){
my $hash{$key}= $w;
}
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Get variable name from a list
2010/7/24 Mike Martin <redtux1 [at] gmail.com>:
> Is this possible?
>
> I am trying to populate a hash with keys as variable name and value as
> variable value eg:
> my %hash;
> foreach my $w ($filename,$file_start,$file_time,$video_track,$audio_track=
,$quality,$sync){
> my $hash{$key}=3D =A0$w;
>
That would be a hash slice:
[at] hash{$filename,$file_start,$file_time,$video_track,$audio_t rack,$quality,$=
sync}
=3D ();
see perldoc perldata:
http://perldoc.perl.org/perldata.html
--
Jeff Pang
http://home.arcor.de/pangj/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Get variable name from a list
>>>>> "JP" =3D=3D Jeff Pang <pangj [at] arcor.de> writes:
JP> 2010/7/24 Mike Martin <redtux1 [at] gmail.com>:
>> Is this possible?
>>
>> I am trying to populate a hash with keys as variable name and value as
>> variable value eg:
>> my %hash;
>> foreach my $w ($filename,$file_start,$file_time,$video_track,$audio_tr=
ack,$quality,$sync){
>> my $hash{$key}=3D =A0$w;
>>
JP> That would be a hash slice:
JP> [at] hash{$filename,$file_start,$file_time,$video_track,$audio_t rack,$qua=
lity,$sync}
JP> =3D ();
i don't see the OP asking for a slice. in fact i don't understand what
he is actually asking for.
OP: that code is legal but overwrites each value in the hash with the
next one as the $key stays the same. try to restate your question or
better yet, backup and explain what your goal is and not ask how to code
it.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Get variable name from a list
2010/7/24 Mike Martin <redtux1 [at] gmail.com>:
>>
>
> This is how I worked round the issue (didn't think of hash slice)
>
> foreach my $w (qw/$file_start $file_time $video_track $audio_track
> $quality $sync $sync_box $qual_box $vid_box $aud_box $time_label
> $times $file_hbox/) #Use qw to turn variable name into a string
>
> {
> my $key=3D =A0$w;
> my $value=3D$w;# assign $w to value and key
> $key=3D~s/^\$//; # remove $ sign from key
> $value=3Deval('\\'.$value); # turn variable name into a reference
> pointing to original variable
>
> $gui_hash{$key}=3D$$value ; create hash with and dereferenced value as va=
lue
> }
>
Mike,
Please post the question to the list, not just to me, that will help
you get better helps.
--
Jeff Pang
http://home.arcor.de/pangj/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Get variable name from a list
> ...
> 2010/7/24 Mike Martin <redt... [at] gmail.com>:
>
> This is how I worked round the issue (didn't think of hash slice)
>
> foreach my $w (qw/$file_start $file_time $video_track $audio_track
> $quality $sync $sync_box $qual_box $vid_box $aud_box $time_label
> $times $file_hbox/) #Use qw to turn variable name into a string
>
> {
> my $key=3D =A0$w;
> my $value=3D$w;# assign $w to value and key
> $key=3D~s/^\$//; # remove $ sign from key
> $value=3Deval('\\'.$value); # turn variable name into a reference
> pointing to original variable
>
> $gui_hash{$key}=3D$$value ; create hash with and dereferenced value as v=
alue
> }
Your use of string eval is slow - especially in a loop -
since the string must compiled. Potentially dangerous
too if there's any user input involved. (You also store
a ref to $value unnecessarily since you just deref it
during the hash assignment later.)
Here's an example of the faster, safer hash slice that
was mentioned:
my [at] var_names =3D qw/ file_start, file_time ... /;
my [at] var_values =3D ( $file_start, $file_time, ... );
my %gui_hash;
[at] gui_hash{ [at] var_names } =3D [at] var_values;
Might as well just sync up names,values directly
in the hash declaration:
my %gui_hash =3D ( file_start =3D> $file_start,
file_time =3D> $file_time,
...
);
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/