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

Re^5: 5.26 sigil reference syntax in subfunction

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


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

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.

Replies are listed 'Best First'.
Re^6: 5.26 sigil reference syntax in subfunction
by chenhonkhonk (Acolyte) on Nov 02, 2017 at 11:38 UTC
    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; }
      That example is neither short nor complete, so once again I'll have to speculate about what it is you're trying to achieve.

      In the following code:

      sub f { my ($r) = @_; $r->{level3} = 1; } my $rh; f($rh->{level1}{level2});
      I'm guessing you'd like it to autovivify all three levels of hash, so that the effect is the same as
      rh->{level1}{level2}){level3} = 1;
      If so, that can be achieved with
      f(\$rh->{level1}{level2}->%*);

      Dave.

        use strict; use warnings; my @options_lines = (); my %files = (); my $key = ""; open (OPTIONSFILE, "<", ".\\include\\options.h"); while (<OPTIONSFILE>){ push ( @options_lines,"$_"); } close (OPTIONSFILE); assign_column_names_to_file( \@options_lines, \%{ $files{'options'}{'c +olumns'} } ); foreach $key (sort keys $files{'options'}{'columns'}{'config'}->%* ){ print "columns: $key\n"; } print "\nExiting\n"; exit 0; ############################################################# sub assign_column_names_to_file{ # \@options_lines, $file{'options'}-> +{columns} (hash reference) ##rewrite based on difopt 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++) { 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.*/ ){ next; } else { $$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); $$columnhash{'config'}{"$config_names[$i]"}{'r +owstates'}{$j} = substr($$options_lines[$j], $horizontal_offset + $i +* 2, 1); } } } } foreach $key ( keys $$columnhash{'column number'}->%* ){ #foreach $key (sort keys %{ $$columnhash{'config'} } ){ print "$key\n"; } return 0; }

        This is literally the entire code. Not sure what more you were expecting. I have left in accessing inside and outside the sub to demonstrate the inconsistent syntax. Particularly inside the function, the new syntax is not helpful, and is more than confusing because I have to pay attention to the front and back of hashes.

        Additionally, in upper function, I must use \%{} if I want to pass the hash reference - and it must be bracketed because of precedence compared to the first $.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2019-09-21 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (269 votes). Check out past polls.

    Notices?