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

Yes, I am confused. Just starting to play with perl and trying to do something clever (for me, anyway).

Basically, trying to extract this string:
server1 server2 server3

from this text:
server1:NY:3600 server2:NY:3600 server3:NY:3600 server4:Boston:3600
Easy (even for me) however IF there is a "webserver#" entry, I want different results. I want this string:
"webserver1 server2 server3 webserver4"

from this:
server1:NY:3600 webserver1:NY:3600 server2:NY:3600 server3:NY:3600 webserver4:NY:3600 server4:Boston:3600
In other words, extract "webserverx" but NOT "serverx"

There is probably a hundred ways of doing this, but every time I go down a path, I get stuck. I'm getting very frustrated. I know I could do this with a shell script, but I really want to start using perl. This is my last resort.

Any help with where to start will be greatly appreciated. This is what I have so far. Am I going down the right path?:

#!/usr/bin/perl # stuff use warnings; use strict; open(SRVS, "< /opt/etc/servers") || die "Can't open /opt/etc/servers"; my $wserv = 0; my @allserv = (); while (my $line = <SRVS>) { if ($line =~ /server/) { if ($line =~ /web/) { @webserv = split(":", $line); $wserv = 1; push(@allserv, $webserv[0]); } else { $wserv = 0; } } }

Replies are listed 'Best First'.
Re: Data manipulation confusion
by ikegami (Patriarch) on Mar 10, 2010 at 21:42 UTC
    It helps to think about what you want to do.
    • If you find webserverX, you want to forget that you've seen serverX.
    • If you find serverX, you want to ignore it if you've seen webserverX.

    Then coding becomes simple.

    my %servers; while (<>) { my ($server) = /^([^:]+):/ or next; delete($servers{$1}) if $server =~ /^web(.*)/; $servers{$server} = 1 if !$servers{"web$server"}; } my @servers = keys(%servers);

      See, I would never have thought to look at it in those terms. My challenge is not so much to learn perl, but to change my way of thinking. If I hang around here, hopefully it will seep into my thick skull.

      Thank you so much. This worked perfectly.

        I would never have thought to look at it in those terms.

        I'm not sure what terms you have picked. You didn't really describe what you wanted done. "I want this from that" requires guesswork to understand. Specifically, it doesn't answer what's special about the lines you want to keep. If you come up with a different answer to that question, you'll probably get a different program that gets the same result.

Re: Data manipulation confusion
by raghu_shekar (Novice) on Mar 11, 2010 at 04:54 UTC
    hi there. You could just use the perl pattern matching to achieve this. it s very simple and easy. Just incorporate this below piece of code on your array.
    @webserverlist = qw(server1:NY:3600 webserver1:NY:3600 server2:NY:3600 + server3:NY:3600 webserver4:NY:3600 server4:Boston:3600); foreach (@webserverlist) { if ( $_ =~ m/^web.*/ ) { push (@list, $_."\n" ); } } print @list;
    this will get all the WEB*servers into the array @list. enjoy scripting......
      server2 and server3 are not in the output as they should be.

      Update: s/are in/are not in/