in reply to Hash Ref Error

Untested (I'm on my phone): $my_data_ref->{names};

I'll test and explain if nobody else does shortly.

Update: There's nothing technically wrong with your code, and it should work (as the Monks who tested it below can show). However, it isn't the most idiomatic Perl out there. Right off the bat, although perl allows whitespace nearly anywhere, it's clearer if you keep your delimiters together: ${ $my_data_ref }{ names }

What this does:

${ $my_data_ref } { names }

is it dereferences $my_data_ref hash reference, and converts (actually it forces) the value(s) of its names key into a scalar value. It is equivalent to the more idiomatic:

my $scalar = $my_data_ref->{names}

Both do the same thing, but one is a bit more understandable at-a-glance, so long as the left-hand side (or within another line or two) makes it clear what type (scalar or list) you're pulling out.

-stevieb

Replies are listed 'Best First'.
Re^2: Hash Ref Error
by Maresia (Beadle) on Sep 15, 2015 at 21:43 UTC
    I would do this way:
    # Create a reference to an anonymous hash with the data my $my_data = { names => $some_data }; #my $my_data_ref = \%my_data; my @all_names = split/\n/, ${ $my_data } { names }; foreach my $rows (@all_names){ print $rows."\n"; } ...

      Excellent Maresia! Here's a couple of different ways you can do it. Note that double-quotes automatically 'interpolate' variables (ie. expands variables to their values), so you can even do "$rows\n"; instead of $rows."\n" :)

      my %my_data = ( names => $some_data); my $my_data_ref = \%my_data; my @all_names = split /\n/, $my_data_ref->{names}; for (@all_names){ print "$_\n"; } # or even skip creating the @all_names array altogether print "$_\n" for split /\n/, $my_data_ref->{names};

      I wouldn't recommend the latter unless you're really just doing something with the extracted information immediately on the spot.