FAQ
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;

}

Search Discussions

  • Jeff Pang at Jul 24, 2010 at 9:50 am

    2010/7/24 Mike Martin <redtux1@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}=  $w;
    That would be a hash slice:

    @hash{$filename,$file_start,$file_time,$video_track,$audio_track,$quality,$sync}
    = ();
    see perldoc perldata:
    http://perldoc.perl.org/perldata.html
  • Uri Guttman at Jul 24, 2010 at 10:11 am
    "JP" == Jeff Pang writes:
    JP> 2010/7/24 Mike Martin <redtux1@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}=  $w;
    >>

    JP> That would be a hash slice:

    JP> @hash{$filename,$file_start,$file_time,$video_track,$audio_track,$quality,$sync}
    JP> = ();

    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@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Jeff Pang at Jul 24, 2010 at 1:53 pm

    2010/7/24 Mike Martin <redtux1@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=  $w;
    my $value=$w;# assign $w to value and key
    $key=~s/^\$//; # remove $ sign from key
    $value=eval('\\'.$value); # turn variable name into a reference
    pointing to original variable

    $gui_hash{$key}=$$value ; create hash with and dereferenced value as value
    }
    Mike,

    Please post the question to the list, not just to me, that will help
    you get better helps.
  • C.DeRykus at Jul 25, 2010 at 9:58 am

    ...
    2010/7/24 Mike Martin <redt...@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=  $w;
    my $value=$w;# assign $w to value and key
    $key=~s/^\$//; # remove $ sign from key
    $value=eval('\\'.$value); # turn variable name into a reference
    pointing to original variable

    $gui_hash{$key}=$$value ; create hash with and dereferenced value as value
    }
    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 @var_names = qw/ file_start, file_time ... /;
    my @var_values = ( $file_start, $file_time, ... );

    my %gui_hash;
    @gui_hash{ @var_names } = @var_values;

    Might as well just sync up names,values directly
    in the hash declaration:

    my %gui_hash = ( file_start => $file_start,
    file_time => $file_time,
    ...
    );

    --
    Charles DeRykus

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJul 23, '10 at 4:15p
activeJul 25, '10 at 9:58a
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase