LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl string replacement within an array? (https://www.linuxquestions.org/questions/programming-9/perl-string-replacement-within-an-array-227821/)

Seventh 09-07-2004 02:50 PM

Perl string replacement within an array?
 
I have an array that gets it's data by stripping version files.

So the array looks sort of like:

mtasd modulew foo1 bar2 etc1 etc2

Is there a way that I can manipulate those, yet hold their place in the array?

What I'd like to do is define some "real" names for the modules in the array, so that if in a loop:

- I print out $var
- The loop's on it's third pass
- The third string in the array is "foo1"

I'd like to define "foo1" as "Foo Program Number 1".

What I do is loop two arrays, and print them in sequence. One's the module name, one's the version number.

Right now it outputs as:

foo1 1.2.3.4

If I can add some kind of replacement variable for "foo1", I could output my data as:

Foo Program Number 1 (is) 1.2.3.4

Any insight appreciated. :)

Seventh 09-07-2004 02:50 PM

Here's the full script btw, in case I'm completely explaining it wrong. :)

Code:


system("clear");

print "CVS Version Check 1.0 - Must be run in CVSROOT\n";
print "----------------------------------------------\n\n";
print "Would you like to update the CVS tree (Recommended) [Y/N]?" ;
$update = <STDIN>;
chomp ($update);

$i = 0;                                       
while ($i == 0) {
if ($update eq "Y") {
    $i = $i + 1;
    system ("clear");
    print "Updating CVS tree, standby...\n";   
    qx (cvs update -A);
 
}elsif ($update eq "y") {
    $i = $i + 1;
    system ("clear");
    print "\nUpdating CVS tree, standby...\n";   
    qx (cvs update -A);
   
} elsif ($update eq "N") {
    $i = $i + 1;
    print "\n\> \> Continuing without update.\n";

} elsif ($update eq "n") {
    $i = $i + 1;
    print "\n\> \> Continuing without update.\n";
                                                   
} else {
    print "Please answer (Y)es or (N)o [Y/N]:";
    $update = <STDIN>;
    chomp ($update);
    }
}

print "\n\> \> Tree manipulation complete. \n\n";

print "(S)ingle module lookup, or (C)omplete tree? [S/C]?" ;
$lookupchoice = <STDIN>;
chomp ($lookupchoice);

$lc = 0;

while ($lc == 0) {
    if ($lookupchoice eq "S") {
        $lc = $lc + 1;
        print "\nEnter Module to Query: ";
        $singlemod = <STDIN>;
        chomp ($singlemod);

        @singlemodarray = <${singlemod}_v.pm>;
        @singlemodarray = glob ("${singlemod}_v.pm");

        my $scnt=0;
        foreach $sf (@singlemodarray){
            $sver = qx(tail -1 ${singlemod}_v.pm);


            my ($singlename) = $sf =~ /(.*)_v\.pm/; 
            my $srawver = "unknown";
            if ($sver=~ /our \$Version\=/) {
                ($srawver) = $sver =~ /\$Version\=\"(.*)\"\;/;
            } else {
                $srawver = "No version information available";
            } 
            print "\nModule: $singlename\t Version: $srawver\n";
            $scnt++;
        }
        $scnt = ($scnt);
         
} elsif ($lookupchoice eq "s") {
    $lc = $lc + 1;
        print "\nEnter Module to Query: ";
        $singlemod = <STDIN>;
        chomp ($singlemod);

        @singlemodarray = <${singlemod}_v.pm>;
        @singlemodarray = glob ("${singlemod}_v.pm");

        my $scnt=0;
        foreach $sf (@singlemodarray){
            $sver = qx(tail -1 ${singlemod}_v.pm);

            my ($singlename) = $sf =~ /(.*)_v\.pm/; 
            my $srawver = "unknown";
            if ($sver=~ /our \$Version\=/) {
                ($srawver) = $sver =~ /\$Version\=\"(.*)\"\;/;
            } else {
                $srawver = "No version information available";
            } 
            print "\nModule: $singlename\t Version: $srawver\n";
                                         
            $scnt++;
        }
        $scnt = ($scnt);
           
} elsif ($lookupchoice eq "C") {
    $lc = $lc + 1;
    @module = <*_v.pm>;
    @module = glob("*_v.pm");

    my $cnt=0;
    foreach $f (@module){
        $ver = qx(tail -1  $f);
        my ($name) = $f =~ /(.*)_v\.pm/; 
        my $rawver = "unknown";
        if ($ver=~ /our \$Version\=/) {
        ($rawver) = $ver =~ /\$Version\=\"(.*)\"\;/;
    } else {
        $rawver="No version information available";
    } 
        print "Module: $name\t Version: $rawver\n";
        $cnt++;
}
    $cnt = ($cnt - 1);
    print "Total Modules: $cnt \n";

} elsif ($lookupchoice eq "c") {
    $lc = $lc + 1;
    @module = <*_v.pm>;
    @module = glob("*_v.pm");

    my $cnt=0;
    foreach $f (@module){
        $ver = qx(tail -1  $f);
        my ($name) = $f =~ /(.*)_v\.pm/; 
        my $rawver = "unknown";
        if ($ver=~ /our \$Version\=/) {
        ($rawver) = $ver =~ /\$Version\=\"(.*)\"\;/;
    } else {
        $rawver="No version information available";
    } 
        print "Module: $name\t Version: $rawver\n";
        $cnt++;
}
    $cnt = ($cnt - 1);
    print "Total Modules: $cnt \n";

} else {
        print "Please type S for single, or C for complete [S/C]: ";
        $lookupchoice = <STDIN>;
        chomp ($lookupchoice);
    }
}

print "\n\> \> Finished\n";     
                                 
;exit



All times are GMT -5. The time now is 09:34 PM.