LinuxQuestions.org
Visit Jeremy's Blog.
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 08-31-2004, 11:20 AM   #1
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Rep: Reputation: 30
Perl array (newbie) question -text replacement


I'd like to replace some text in an array output. My code is:

Code:
#!/usr/bin/perl

$dirlist = qx(ls $dir);
@complete = split ("\n",$dirlist);

@versions = <*_v.pm">;
@versions = glob("*_v.pm");

my $cnt=0;
foreach $f (@versions){
# print "$cnt $f\n";
$ver = qx(tail -1  $f);
print "$cnt $f $ver\n";
$cnt++;
}
The _v.pm files are version control for CVS. The output of this script looks like:

Code:
0 fbcb2d_v.pm our $Version="0.6.10-Experimental (2004.08.20 14:37)";

1 logd_v.pm our $Version="1.0.8 (2004.08.20 14:34)";
The "our $Version=" is in almost every file. What I'd like to do is replace with with the text "Current Version", and if it's not there, replace $ver with "No version information available".

Any insight appreciated. Thanks.


Last edited by Seventh; 08-31-2004 at 12:29 PM.
 
Old 09-01-2004, 09:07 AM   #2
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Original Poster
Rep: Reputation: 30
^
 
Old 09-01-2004, 10:57 AM   #3
scissors
Member
 
Registered: Aug 2004
Location: Seattle
Distribution: Red Hat es and as, Solaris, Fedora, Debian, Tru64
Posts: 45

Rep: Reputation: 15
You can say:

Code:
if ($ver=~ /our \$Version\=/){
   $ver=~ s/our \$Version\=/Current Version/;
}else{
   $ver=~ s/our \$Version\=/No version information available/;
}
 
Old 09-01-2004, 10:58 AM   #4
scissors
Member
 
Registered: Aug 2004
Location: Seattle
Distribution: Red Hat es and as, Solaris, Fedora, Debian, Tru64
Posts: 45

Rep: Reputation: 15
I meant this:

Code:
if ($ver=~ /our \$Version\=/){
   $ver=~ s/our \$Version\=/Current Version/;
}else{
   $ver="No version information available";
}
 
Old 09-02-2004, 10:24 AM   #5
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Original Poster
Rep: Reputation: 30
I trimmed it up a ton and gave that a shot, but I think I'm missing something on the syntax side.

Here's the full script (minus header)

Code:
system("clear");                                #Clear the screen
print "Current CVS Module Versions\n\n";

@module = <*_v.pm>;
@module = glob("*_v.pm");

my $cnt=0;
foreach $f (@module){
# print "$cnt $f\n";
    $ver = qx(tail -1  $f);
    my ($name) = $f =~ /(.*)_v\.pm/;   
    if ($ver=~ /our \$Version\=/){
    my ($rawver) = $ver =~ /\$Version\=\"(.*)/;
    }else{
    $ver="No version information available";
}  
    print "$cnt $name $rawver\n";
    # push (@version,$ver,);

    $cnt++;
}

print "\nFinished -[ Hit Enter ]\n";             #
$_ = <STDIN>;                                    # Blank Variable for CR
                                                 #

The tail command I use gives this:

$ tail -1 adocsd_v.pm
our $Version="2.0.9 (2004.08.20 14:34)";

The whole thing gives me this error:

Code:
0 adocsd
Use of uninitialized value in concatenation (.) or string at version.pl line 41.
Line 41 is this:

Code:
    print "$cnt $name $rawver\n";
Thanks very much for the input.
 
Old 09-02-2004, 12:11 PM   #6
scissors
Member
 
Registered: Aug 2004
Location: Seattle
Distribution: Red Hat es and as, Solaris, Fedora, Debian, Tru64
Posts: 45

Rep: Reputation: 15
I think it is because you are declaring some variables in the if statement. They will not get initialized if you don't meet the "if" criteria.

So instead of this:

my ($rawver) = $ver =~ /\$Version\=\"(.*)/;

put this up near the top:

my ($rawver);

and then make your statement look like this:

$rawver = $ver =~ /\$Version\=\"(.*)/;
 
  


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
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM
Perl string replacement within an array? Seventh Programming 1 09-07-2004 02:50 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
finding the length of an array in perl acid_kewpie Programming 5 09-25-2002 10:50 AM
perl split(@array)?? wtf? bobjones Programming 5 09-23-2002 09:41 AM

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

All times are GMT -5. The time now is 06:37 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