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

Re^3: hash & arrays

by colwellj (Monk)
on Nov 02, 2009 at 23:04 UTC ( [id://804578]=note: print w/replies, xml ) Need Help??


in reply to Re^2: hash & arrays
in thread hash & arrays

Spooky,

you can always explicitly define the type so instead of $linex (a pointer to an array) you can use "@{$linex}", my SCALAR is actually a list, honest.

cheers

John

Replies are listed 'Best First'.
Re^4: hash & arrays
by Spooky (Beadle) on Nov 04, 2009 at 13:11 UTC
    I tried the the following: @hash{$emp_id} = split(/a-z{2,5}A-Z{2,5}/,$linex); Now, this splits "linex" very nicely but I'm not sure how to access individual "elements" with the split. I know how to do this with an array but not sure about the syntax required using a hash structure.
      Spooky, try this.
      my %hash = []; #explicitly create a hash of arrays structure for clari +ty #push your items into the array - this will create the hash $userid if + it dosent already exist push @{$hash{$userid}}, ($title, $adjst, $adjst2, @{$linex}) #@{$linex) says i am a list #then to get a particular emement print OUTPUT $hash{$userid}[0]; #prints $title for that user
      John

        Your example code isn't doing what you're saying it is...

        my %hash = []; #explicitly create a hash of arrays structure for clari +ty

        This doesn't mean anything, and is in fact an error; you can't assign a single scalar (an array reference in this case) to a hash.

        #push your items into the array - this will create the hash $userid if + it dosent already exist push @{$hash{$userid}}, ($title, $adjst, $adjst2, @{$linex}) #@{$linex) says i am a list

        $userid isn't a hash. %hash is. $hash{$userid} is a hash element. In saying @{$hash{$userid}} you're asking perl to treat that hash element as an array reference, and dereference it; in this case, the element doesn't exist, and so is created as what it ostensibly is (an array ref), and then used via push to push those elements onto it. The parenthesis are unnecessary and don't mean anything here.

        Also, $linex in his code sounds like a string, not an array ref.

        thanks John - but what if I want to pull an element from $linex which I've broken up using the split option - that's what I'm really after....Spooky ps - Spooky is the name of my mini-cooper s (yeah - it's black!) named after one of my favorite songs performed by Classics 4 and recently recorded by Imogen Heap..just an fyi... John, When I run the following: %hash = [] ; $title = 'engineer' ; $usr = 'ewh1234' ; @mylist = (1, 2, 3) ; push @{$hash{$usr}}, ($title, @{$mylist}) ; print "$hash{$usr} [0]" ; I get the following: ARRAY(0x2001dbe0) [0] I was expecting 'engineer' - of course what I really want is to access an element with @mylist....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-24 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found