Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Dynamic population of a hash

by JFarr (Sexton)
on Nov 08, 2005 at 21:47 UTC ( #506907=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I have a question on how to build a hash from a string. I've searched the O'Reilly book and the Categorized Questions here, but nothing really explains how to do what I would like to do. I'll have already read in a file that contains a string of partial directories, ie. /Workstation_X_X/<someotherpath>/ How would I set this up to populate a hash? Thanks in advance. Perl NooBie

Replies are listed 'Best First'.
Re: Dynamic population of a hash
by friedo (Prior) on Nov 08, 2005 at 21:49 UTC
    Populate a hash with what?
Re: Dynamic population of a hash
by ikegami (Patriarch) on Nov 08, 2005 at 21:53 UTC
    Hashes contain key => value pairs, yet you only mentioned a single value ("a string of ..."). Could you please provide more information?
Re: Dynamic population of a hash
by GrandFather (Saint) on Nov 08, 2005 at 22:02 UTC

    As others have said you've not supplied enough information, but this may get you started:

    use strict; use warnings; use Data::Dumper; my @strs = ( '/Workstation_X_X/<someotherpath>/', '/Workstation_X_X/<someotherpath>/', '/Workstation_X_X/<someotherpath>/filename.yyy' ); my %unique; @unique {@strs} = (); print Dumper (\%unique);


    $VAR1 = { '/Workstation_X_X/<someotherpath>/' => undef, '/Workstation_X_X/<someotherpath>/filename.yyy' => undef };

    Perl is Huffman encoded by design.
      Forgive me, I'll be more specific. With the strings that will be read in from the file, how would I make the the mappings dynamically.. So if the first $T is /Workstation_X_X/<someotherpath>/, would I do something like:
      foreach $T (@myfile) { %map = {'first', X, 'second', Y, 'third', Z); }
      Now somes the other question, being so new to perl, how do would you parse the "/Workstation_X_X/<someotherpath>/" strings into the X,Y,Z? Using reg expression? grep? some magic?
        Your question is still remarkably unclear. What is X, Y and Z? In your filename, all I see are a couple Xes.

        If you've got an ordered list of data, (which is what I surmise from your keynames of 'first', 'second', and 'third'), why are you using a hash instead of an array?

        Be very specific about what you're trying to accomplish. No more nonsensical sample data. Show us what you have and what you're trying to do with it and what code you've written.

        Give us three sample strings and what you expect to see in the hash.

        BTW, I'd tend to avoid map as a name in the same way I'd tend to avoid grep and and and for as names.

        Perl is Huffman encoded by design.
        You can use split to split the path up on the slashes, like so:
        my @values = split m:/:, $path_string;
        It's not at all clear why you'd be putting that into a hash as you illustrate. It would be more interesting to make a multilevel hash that mimics the hierarchy of a list of paths:
        my $hashed_path = {}; for my $path_string (@list_of_paths) { my $hpref = $hashed_path; for $dir (split m:/:, $path_string) { $hpref->{$dir} = {} unless exists $hpref->{$dir}; $hpref = $hpref->{$dir}; } }
        That might be a bit much to absorb so early in your Perl experience, but maybe not.

        Caution: Contents may have been coded under pressure.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2023-05-30 17:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found