Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Hash Ref Error

by Maresia (Beadle)
on Sep 15, 2015 at 21:43 UTC ( [id://1142140]=note: print w/replies, xml ) Need Help??


in reply to Re: Hash Ref Error
in thread Hash Ref Error

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"; } ...

Replies are listed 'Best First'.
Re^3: Hash Ref Error
by stevieb (Canon) on Sep 15, 2015 at 22:54 UTC

    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-23 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found