Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: 5.26 sigil reference syntax in subfunction

by dave_the_m (Monsignor)
on Nov 01, 2017 at 12:05 UTC ( #1202516=note: print w/replies, xml ) Need Help??


in reply to Re^2: 5.26 sigil reference syntax in subfunction
in thread 5.26 sigil reference syntax in subfunction

I didn't understand your reply at all. I showed a line of code that was almost identical to the line you were having problems with, but which exhibited the behaviour you appear to have wanted (dumping the second-level keys of multidimensional hash). If this is insufficient, you'll have to describe in much more detail what it is you're trying to achieve.

Dave.

  • Comment on Re^3: 5.26 sigil reference syntax in subfunction

Replies are listed 'Best First'.
Re^4: 5.26 sigil reference syntax in subfunction
by chenhonkhonk (Acolyte) on Nov 01, 2017 at 16:44 UTC

    It's multidimensional in variables. An example line is:

    $$columhash{'config'}{"config_names[$i]"}{'rowstates'}{$j} = substr($$options_lines[$j], $horizontal_offset + $i * 2, 1);

    If I want to immediately print the value, I can do:

    print $$columhash{'config'}{"config_names[$i]"}{'rowstates'}{$j}; or print $$columhash{'config'}->{"config_names[$i]"}->{'rowstates'}{$j};

    For the foreach (still in the function), if I want what the config_names$ were, I can do:

    foreach $key (sort keys $columnhash->('config')->%* )

    or

    foreach $key (sort keys %{ $columnhash->('config') } )

    or even

    foreach $key (sort keys %{ $$columnhash{'config'} } )

    but not

    foreach $key (sort keys %$columnhash->('config')

    and more annoyingly, when I call the function with:

    assign_columns_to_file( \@options_lines, $files{'options'}{'columns'} );

    It will keep 'options' as a subkey, but will not keep 'columns' used during the function (presumably because of passing an undef) So I figure to see if explicitly referencing it will work, but I can't use ->\%, I can only use \%{ } i.e.:

    assign_columns_to_file( \@options_lines, \%{ $files{'options'}{'columns'} } ); Ultimately I end up having to use the surrounding referencing. I'm splitting things more into one-off functions, but I'll still end up with foreachs and \%{} running around everywhere.
      assign_columns_to_file( \@options_lines, $files{'options'}{'columns'} );

      It will keep 'options' as a subkey, but will not keep 'columns' used during the function

      Again, I don't understand what you are trying to say here. Can you please give a short, but complete self-contained example program that demonstrates the issue.

      Dave.

        assign_column_names_to_file( \@options_lines, \%{ $files{'options'}{'c +olumns'} } ); foreach $key (sort keys $files{'options'}{'columns'}{'config'}->%* ){ print "columns: $key\n"; } ############################################################# sub assign_column_names_to_file{ # \@options_lines, $file{'options'}-> +{columns} (hash reference) my ($options_lines, $columnhash) = @_; #array ref, hash ref # this function populates a reference to a hash which has all the +column names, numbers, and states my $regex__cfg_names = qr"^(\/\*\+\*\/\s+\/\*\|)(.*)\*\/\r?$"; my $horizontal_offset = 116; #magic number, could calculate dynami +cally #my $horizontal_offset = 0; my @letters = (); my @config_names = (); my @columns = (); my $i = 0; my $j = 0; my $line; foreach $line (@$options_lines) { # read config names at top of file into $config_name if ($line =~ m/${regex__cfg_names}/) { #$horizontal_offset = length($1); #print (length($1 . "\n") - 1); undef @letters; @letters = split (/ ?/, $2); for ($i = 0; $i < @letters; $i++) { if( $letters[$i] ne " " ){ $config_names[$i] .= $letters[$i]; } #print STDERR "\nConfig Name:".$config_names [$i]; } } } for( $i = 0; $i < @config_names ; $i++){ if( $config_names[$i] =~ m/.*ooo.*/ ){ #print "skipping $config_names[$i], column " . ($horizonta +l_offset + $i * 2) . "\n"; next; } else { #print "trying to add $config_names[$i], column " . ($hori +zontal_offset + $i * 2) . "\n"; $$columnhash{'config'}{"$config_names[$i]"}{'column number +'} = $horizontal_offset + $i * 2; $$columnhash{'column number'}{$horizontal_offset + $i * 2} +{'config'} = "$config_names[$i]"; for( $j = 0; $j < @$options_lines; $j++){ if ( (length $$options_lines[$j] > $horizontal_offset ++ $i * 2 ) && (substr($$options_lines[$j], $horizontal_offset + $i * +2, 1) ne " ") ){ $$columnhash{'column number'}{$horizontal_offs +et + $i * 2}{'states'}{$j} = substr($$options_lines[$j], $horizontal_ +offset + $i * 2, 1); #print $columnhash->{'column number'}->{$horiz +ontal_offset + $i * 2}->{'states'}{$j}; $$columnhash{'config'}{"$config_names[$i]"}{'r +owstates'}{$j} = substr($$options_lines[$j], $horizontal_offset + $i +* 2, 1); } } } } # #foreach $key ( keys $$columnhash{'column number'}{$horizontal_of +fset + $i * 2}->%* ){ # foreach $key (sort keys %{ $$columnhash{'config'} } ){ # print "$key\n"; # } return 0; }
Re^4: 5.26 sigil reference syntax in subfunction
by chenhonkhonk (Acolyte) on Nov 01, 2017 at 18:10 UTC
    Sorry for the duplicate post, I didn't notice the minimizing.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2019-09-15 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (180 votes). Check out past polls.

    Notices?