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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You don't really push to a hash, you can - but push is usually reserved for array (and yes, a hash is an array). My point is that you assign a value to a hash key - that value can be a scalar (the value), a reference (the pointer), or other things.

Here is an example that reads 'configuration info' from Perl's built in DATA filehandle. I use DATA because it's easy and you only need one file for demonstrations. Anyhow, the configuration is simply two values seperated by a comma per line. The while loop processes DATA one line at a time and creates a new struct object with the values it finds. After the struct is instantiated, it is pushed to an array. Since it is already a reference there is no need to de-reference it. The last line is a for loop that prints out only one of the attributes for each struct in the array.

use strict; use Class::Struct; # declare the struct struct(MyStruct => { even => '$', odd => '$', }); # create array of structs my @structs; while (my $line = <DATA>) { chomp $line; my ($odd,$even) = split(',',$line); my $obj = new MyStruct; $obj->odd($odd); $obj->even($even); push(@structs,$obj); } print $_->odd(), "\n" foreach (@structs); __DATA__ one,two three,four five,six seven,eight nine,ten
But hold it right there! Why are you using Class:Struct? No offense to the author, but if you are going to code in Perl, code like Perl. Check this out:
use strict; # create array of structs my @structs; while (my $line = <DATA>) { chomp $line; my ($odd,$even) = split(',',$line); my %hash = ( odd => $odd, even => $even, ); # i do have to de-reference %hash though push(@structs,\%hash); } print $_->{'odd'}, "\n" foreach (@structs); __DATA__ one,two three,four five,six seven,eight nine,ten
Same thing with less lines, and i spared using map to golf it down even further ... oh what the hell:
print $_->{odd},"\n"for map{my($o,$e)=split',',$_;{odd=>$o,even=>$e}}< +DATA>;
Welcome to Perl. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

In reply to (jeffa) 3Re: Dynamically allocating variables by jeffa
in thread Dynamically allocating variables by newatperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 15:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found