Hello and thanks for taking the time to look at my issue.

I have a script that I'm working on that handles setup of new servers, via user input. The user enters server information which is then stored in a hash. The hash keys all start with 'node'.

The issue is that the hash entries are stored as node0-9wwn0, etc.

Sample:

Key: node0 Value: servername0 Key: node0wwn0 Value: servername0wwn0 ...
What I need is to pull JUST the node key (which is variable based on how many servers are entered...could be node0-node199).

If I regex match on node0-9 inside the loop it pulls the last hash entry (node0wwn0 in this case), and all I need is 'node0' and it's corresponding key. After several days of searching and getting nowhere with substr and regexes I decided to throw myself on the mercy of the Monks.

foreach my $key (sort keys %$ref) { if ($key ne "Server") { print $key .":" . " " . $ref->{$key} . "\n"; } ### Search the hash for cluster nodes. ### If they exist, perform operations ### Regex looks for the node.* keys, which indicates a cluster node. ### If there's no cluster, there won't be any node.* keys. if ($key =~ m/node.*$/) { print "We have a cluster! Here are the entries: " .$key. ":" . $ref-> +{$key}. "\n"; } if ($key =~ m/node[0-9]/) { print $key . ":" . $ref->{$key}. "\n"; } }
As you can see from above regex, that matches anything on that line with node0-9, when all I want to pull out of the hash is node0-9...my problem is that the key is variable, as I said, so I can't hard code it. Can someone point me in the right direction? Thank you much!


In reply to Partial string match -- with a twist. by smw6181

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.