School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,363 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,450 people online right now. Registration is fast and FREE... Join Now!




Updating hash value

 

Updating hash value

Sun751

25 Jun, 2009 - 02:34 AM
Post #1

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
In below code I am iterating my hash and checking if value of res.ldlibrarypath(key) equals to content of $val,

Actually I want to substitute some of the content of the value so I am doing substitution, But can any one tell me how can I update my hash after that substitution?

CODE

    for my $hash($HR_wnt,$HR_unx,$HR_component)
    {
        for my $id (keys %$hash)
        {
            if ($id eq 'res.ldlibrarypath')
            {
                      $$hash{$id}=~ s/\/opt\/app\/$val.*?://;

             }
        }
    }

Any one have any suggestion, please

Cheers

User is offlineProfile CardPM
+Quote Post


chorny_cpan

RE: Updating Hash Value

25 Jun, 2009 - 02:39 AM
Post #2

New D.I.C Head
Group Icon

Joined: 13 May, 2009
Posts: 35


Dream Kudos: 25
My Contributions
QUOTE(Sun751 @ 25 Jun, 2009 - 02:34 AM) *

how can I update my hash after that substitution?

CODE

                      $$hash{$id}=~ s/\/opt\/app\/$val.*?://;

You are already updating it.

Also , it is better to write
CODE

                      $hash->{$id}=~ s/\/opt\/app\/$val.*?://;


User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Updating Hash Value

25 Jun, 2009 - 03:01 AM
Post #3

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(Sun751 @ 25 Jun, 2009 - 10:34 AM) *

CODE

$$hash{$id}=~ s/\/opt\/app\/$val.*?://;



In addition to chorny_cpan's comment, you can use pretty much whatever punctuation you want as your regex delimiters (provided you're explicitly stating either the "m" or "s" operator), which can help with avoiding "leaning toothpick syndrome":
CODE

$hash->{$id}=~ s|/opt/app/$val[^:]*:||;

(For more examples of this, including a working demonstration, you can see one of my posts from a couple weeks ago over on PerlMonks.)

I also changed the .*? to [^:]* because it's a clearer statement of what you want ("any number of non-colon characters, followed by a colon" vs. "the smallest possible number of any characters at all, followed by a colon"). It's generally good practice to avoid .* (or .+) in favor of negated character classes whenever possible, even in cases where just making it non-greedy will work.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:25PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month