Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Multidimentional hash

by freekngeek (Acolyte)
on Jul 01, 2013 at 15:50 UTC ( [id://1041878]=perlquestion: print w/replies, xml ) Need Help??

freekngeek has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a text file, from which I have parsed some strings like this :

LB split2 1e38 split1 -0.43092 + (4.788 * W) M2 split2 L < 10 AND W < 1.080 split1 (-0.665364 + (9.6216 * W))*(10/L +) split1 -1.38618 + (20.045 * W) split2(L < 10 AND W >= 1.080) OR (L +>= 10 AND W > 0)split1 -0.665364 + (9.6216 * W) split1 -1.38618 + (20 +.045 * W) M1 split2 L < 10 AND W < 1.080 split1 (-0.665364 + (9.6216 * W))*(10/L +) split1 -1.38618 + (20.045 * W) split2(L < 10 AND W >= 1.080) OR (L +>= 10 AND W > 0)split1 -0.665364 + (9.6216 * W) split1 -1.38618 + (20 +.045 * W)

Now, I want to split this string and want to store it in nested hash..Like this
%metals = ( # $metal-level1 ==> { # LENGTH_RANGES ==> @length(L1,L2) # WIDTH_RANGES ==> @width(W1) # EM_POLY ==> {
<>

LB, M1 and M2 are metal lines and then I have width and length of metals and polynomial equations. I have extract all these values and stored in arrays, But as I am new to perl, So, I couldn't figured out that how can I store those values in 3D hash structure. I am using these keywords split1, split2 to split the string into different arrays for length, widths and so on.

I am not asking for a solution, but just an idea that how can I make a 3d hash structure for the given text. Thanks

Replies are listed 'Best First'.
Re: Multidimentional hash
by hdb (Monsignor) on Jul 01, 2013 at 16:00 UTC

    If you are looking for comments, how to create a multi-dimensional structure, the following might give you a start. With all these nested structures, Data::Dumper is an invaluable tool to inspect them.

    use strict; use warnings; use Data::Dumper; my %metals; $metals{'LB'}{'LENGTH_RANGES'} = [ 0, 10 ]; $metals{'LB'}{'WIDTH_RANGES'} = [ 0, 1.080 ]; $metals{'LB'}{'EM_POLY'} = "(-0.665364 + (9.6216 * W))*(10/L)"; print Dumper \%metals;
Re: Multidimentional hash
by roboticus (Chancellor) on Jul 01, 2013 at 16:09 UTC

    freekngeek:

    First you have to decide/determine what the primary index is. For example, it might be the two character first column or it may be a combination of several columns. You don't give enough information to be sure. (Though your sample data structure indicates that you're using a single key.)

    Lets suppose that the first and second column comprise your key--In that case, you could do something like:

    my %data; while (my $line=<$INPUT_FILE_HANDLE>) { my ($rectype, $testID, $dimX, $dimY, $polynomial) = parse_input_line($line); my $key = "$rectype:$testID"; $data{$key} = { RecType => $rectype, TestID => $testID, DIM => [ $dimX, $dimY ], Polynomial => $polynomial, }; }

    Note the square brackets on the DIM line: that's how you can place an array reference inside your hash.

    Does this answer your question? I don't feel like I understand just what it is you're having difficulty with, so I just guessed. If you could provide a bit more detail, we can give better answers.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Multidimentional hash
by QM (Parson) on Jul 01, 2013 at 16:09 UTC
    As a former Perl newbie, I'd suggest avoiding string literals as hash keys. One typo can take you hours to find, because the "uninitialized value" line number will be where you try to use it, and not where you typoed.

    my $LB = 'LB'; my $LENGTH_RANGES = 'LENGTH_RANGES'; my $WIDTH_RANGES = 'WIDTH_RANGES'; my $EM_POLY = 'EM_POLY'; # ...sometime later... $metals{$LB}{$LENGTH_RANGES} = [ 0, 10 ]; $metals{$LB}{$WIDTH_RANGES} = [ 0, 1.080 ]; $metals{$LB}{$EM_POLY} = "(-0.665364 + (9.6216 * W))*(10/L)";

    Then Perl will check your spelling on the variable names (which are really constants, but that's another kettle of fish).

    But as hdb said, Data::Dumper is invaluable in this area.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Multidimentional hash
by RichardK (Parson) on Jul 01, 2013 at 16:16 UTC

    perldsc the perl data structures cookbook, contains lots of useful info and examples, well worth a read.

Log In?
Username:
Password:

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

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

    No recent polls found