LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2004, 03:50 PM   #1
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Rep: Reputation: 30
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.
 
Old 09-07-2004, 03:50 PM   #2
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 09:06 AM
Shell Script - String Replacement revof11 Programming 7 11-29-2005 07:38 AM
String Array definiation problem nelnel Programming 1 09-16-2005 05:56 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 02:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration