Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Dynamic population of a hash

by JFarr (Sexton)
on Nov 08, 2005 at 22:14 UTC ( #506919=note: print w/replies, xml ) Need Help??

in reply to Re: Dynamic population of a hash
in thread Dynamic population of a hash

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?

Replies are listed 'Best First'.
Re^3: Dynamic population of a hash
by friedo (Prior) on Nov 08, 2005 at 22:19 UTC
    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.

Re^3: Dynamic population of a hash
by GrandFather (Saint) on Nov 08, 2005 at 22:20 UTC

    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.
Re^3: Dynamic population of a hash
by Roy Johnson (Monsignor) on Nov 08, 2005 at 22:45 UTC
    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.

      Thanks everyone. A more concise code sample would be nice, but unfortunately I cannot place this in the public view, so anything I can put out there is gutted etc....But I'll be more attentive when I post other questions and try to make it more complete.

      Could you explain the my $hashed_path={}. Is this some sort of Perl initialization for a variable?

      In my ignorance, how does this make a multilevel hash? I don't see the hast operator in the sample. I understand that PERL does some magic behind the scenes, is this one of those cases?

        {} is an empty hash reference. I could have started with an empty hash, which might have been a little easier to understand, and I should have annotated my code. So I'll do that here:
        # Read in my list of paths and cut off the newlines my @list_of_paths = <DATA>; chomp @list_of_paths; my %hashed_path = (); for my $path_string (@list_of_paths) { # This tracks our descent in the path tree. We start at the top my $hpref = \%hashed_path; # Split the path into its component directories, and walk through th +em for my $dir (split m:/:, $path_string) { # At each level, if we've never been there before, create an entry + for it $hpref->{$dir} = {} unless exists $hpref->{$dir}; # Then descend into it so we're ready to insert the next level $hpref = $hpref->{$dir}; } } # See what the structure looks like use Data::Dumper; print Dumper \%hashed_path; __DATA__ /top/middle/bottom /top/middle/newleaf /top/new_middle/bottom
        Output is
        $VAR1 = { '' => { 'top' => { 'middle' => { 'newleaf' => {}, 'bottom' => {} }, 'new_middle' => { 'bottom' => {} } } } };
        The topmost level has only the key of empty string because that's the first value in a split on '/' when the string starts with '/'.

        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: note [id://506919]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2023-06-06 09:18 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (26 votes). Check out past polls.