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

I use rcs for revision control in my scripts. One such script gets replicated to several client directories on a recurring basis to ensure that they are running the latest code. As a result, I assign a varible as the revision number within rcs:

my $rcs = '$Id:$';

this ends up expanding to a value such as:

$Id: dnsmod,v 1.17 2001/07/20 20:10:14 root Exp root $

I am really only interested in grabbing the 1.17 portion of the string. For now, I am using some dirty code that I really am unsatisfied with:

my $rcs = '$Id: dnsmod,v 1.17 2001/07/20 20:10:14 root Exp root $'; my ($garbarge, $garbage, $version) = split(/ /, $rcs);

This way, I grab $version and stick it within my code in an appropriate place for it to show up and let users match that they are using the newest rev.
I read over the split tutorial and it shows options for assigning multiple values based on the split. I am just hoping that my $garbage variables can be taken out of the mix.
I know that I can use an @array and pull the value with something like:

@array = = split(/ /, $rcs); $version = $array[3];

But are these really my only options?

humbly -c

Replies are listed 'Best First'.
Re: using split to assign a single variable
by lestrrat (Deacon) on Jul 21, 2001 at 01:23 UTC
    my $version = ( split( / /, $rcs ) )[ 3 ];

    should work.

Re: using split to assign a single variable
by lshatzer (Friar) on Jul 21, 2001 at 01:26 UTC
    In the spirit of TIMTOWTDI:

    I use this:  my $version = '$Revision: 1.47 $'; and then I only have to parse out $Revision: and the trailing $'. And that can be done diffrent regex schemes, one method might be: $version =~ s/[^\d.]+//g; (Strip all but digits and .'s).
      And here is one more way to do it! Its based on your use of $Revision:$
      Check it out:

      my($rcs) = (q$Revision: 1.37 $ =~ /(\d+(?:\.\d+)+)/);

      humbly -c

      I missed the forest for the trees on that one. The regex substitution is much easier and I frankly can't believe that I didnt think of it earlier. Talk about being so fixated on an issue that I missed the easy way out.

      VERY humbly -c