Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: who can help me for a very interesting perl program

by Anonymous Monk
on May 03, 2016 at 08:26 UTC ( #1162078=note: print w/replies, xml ) Need Help??


in reply to Re: who can help me for a very interesting perl program
in thread who can help me for a very interesting perl program

thanks choroba and QuillMeantTen. I am still confused. what is its value of "$hash" after performing "unshift @queue , map {$_, $hash} @new_paths;" And I always feel that the value of "$hash" is "undef" all alone, because "map" seems not to be able to assign a value to $hash.

root@TestMachine:~/excerise_perl# perl data_for_path.pl $VAR1 = { 'test' => { 'file2' => undef, 'file1' => undef, 'dir1' => { 'file3' => undef } } };
as the result from above, How does $$data->{'test'} point to its child directory.

Replies are listed 'Best First'.
Re^3: who can help me for a very interesting perl program
by choroba (Archbishop) on May 03, 2016 at 08:32 UTC
    $hash is a hash reference. The referenced hash will get populated in a later iteration of the loop when it becomes $ref. The important thing is that the $hash returned from the do is the same empty hash as the one stored in the @queue by the map.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Hi choroba, for your words "The referenced hash will get populated in a later iteration of the loop when it becomes $ref ". can you explain/demonstrate it in detail:). I think the $hash is "undef" for ever.. Thanks a lot.

        The line:

        unshift @queue, map { [$_, $hash] } @new_paths;

        does indeed put a reference to an empty hash into the queue (note: “empty” here is not the same as undef). In a later iteration, this hash reference is the $ref read from the queue in these lines:

        while (my $next = shift @queue) { my ($path, $ref) = @$next; ...

        At this point, yes, the hash is still empty. But two lines later in the while loop there is this statement:

        $ref->{$basename} = do { ... };

        And that’s where the hash is populated: assigning something (in this case, whatever is returned by the do block) to a hash key is a way of populating the hash. Don’t be confused by the fact that the hash is referenced here by the name $ref rather than $hash. They’re both references, and they both point to the same (anonymous) hash.

        It might help you to visualize what is going on if you display the value of the queue on each iteration of the while loop. Here is the code I used:

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-08-11 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?