Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

What would be the easier way

by zee3b (Novice)
on Mar 04, 2013 at 00:48 UTC ( #1021549=perlquestion: print w/ replies, xml ) Need Help??
zee3b has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, Im trying to parse a file and get an output by giving in house. Example of the code is below

set height "house" set height "house" "string" set height "house" "string" set height "table" "string" set height "table" "string"
Now I can't use a hash table because it won't allow duplicate keys. I want to be able to put out all the strings when I give the input as "house" or the values of height of table when I give in table. So I want to have a group name, "table" that the program will make automatically and then put the strings in that group, an array or something.

The strings have spaces too, but they are in quotation marks. Thanks!

Comment on What would be the easier way
Download Code
Re: What would be the easier way
by james2vegas (Chaplain) on Mar 04, 2013 at 01:00 UTC
    Use a hash table with an array ref for each entry, doing something like push @{ $height{$input} }, $value; after parsing each line into $input and $value; leaving you with something like this:

    %height = ( 'house' => [ string1, string2 ], 'table' => [ string3, string4 ] )
    and @{$height{house}} would be all the values for house in an array
      hey, im sorry i didnt really get that since ive started using perl recently, can you explain this part to me a little @{ $height{$input}} What will my array be called then? Thanks!
        @{ $whatever } de-references the array ref $whatever so you can use the reference as an array. Your data are contained in array references which you need to de-reference with @{ } to use as arrays.
Re: What would be the easier way
by CountZero (Bishop) on Mar 04, 2013 at 02:17 UTC
    I would do it this way:
    use Modern::Perl; use Data::Dump qw/dump/; my %heights; while (<DATA>) { push @{$heights{$1}}, $3 if /set\s+height\s+"([^"]+)"\s*("([^"]+)" +)?/; } say dump(%heights); __DATA__ set height "house" set height "house" "5 meter" set height "house" "10 yards" set height "table" "80 cm" set height "table" "30 inch"
    Output:
    ( "house", [undef, "5 meter", "10 yards"], "table", ["80 cm", "30 inch"], )

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-11-23 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (128 votes), past polls