For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Fw: Reg:need to find the particular pattern in a line and assign to variables.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Fw: Reg:need to find the particular pattern in a line and assign to variables.
Pradeep Reddy

2006-10-30, 7:03 pm

Hello all,

Iam new member to this group and also beginner to PERL.
Here is my question,plz let me know your inpus:
I have a PERL script which gives error report at the end.
Here is the output.

cleartool: Error: Unable to create label "Pradeep" on "/vob/rootinclude/paer.c" version "/main/3".
cleartool: Error: Unable to create label "Pradeep" on "/vob/rootinclude/pcme.h" version "/main/2".

I need to grab the two elements between the two quotes.
Iam very much beginer to the PERL script.
Iam trying this bit of code, not sure how to go on.

foreach (@problemLines) {
push $outLine,@problemLines; /"here Iam trying to get the each line into outLine"/
if ($outLine =~ / cleartool: Error: Unable to create label /) { /"here Iam trying to match the pattern"/
my @values = split(' " ',$outline); /"Here Iam splitting the outline"/
my $pathName =@values[3];
my $Version = @values[5];
cleartool mklabel $RECURSE $REPLACE -version $Version $labelName $pathName; /"This is the comand Iam using"/
print "$_\n";

}




Here Version should get versionof the elements label and pathName should get /vob/root/include/pcme.h
If I use foreach, will it automatically go to next error line, otherwsie doI need to use forloop.

Plz let me now your answers.

Thanks & Regards,
Pradeep.



Find out what India is talking about on - Yahoo! Answers India
Send FREE SMS to your friend's mobile from Yahoo! Messenger Version 8. Get it NOW



________________________________________
__________________
Yahoo! India Answers: Share what you know. Learn something new
http://in.answers.yahoo.com/
Pradeep Reddy

2006-10-30, 7:03 pm

John,

Thx for your inputs.

1st i/p:

Iam afraid, I cant read each line from array problemLines to outLine.

2nd i/p:

=~ /"([^"]+)"/g;------What is the logic in this expression?

Can u bit ellaborate.

I wanted to read the values in double quotes, from each line.

And , Iam using this logic ---if ($outLine =~ / cleartool: Error: Unable to create label /)

, to check for the patten in part of line..Is this correct?

Thanks & Regards,
Pradeep.






----- Original Message ----
From: John W. Krahn <krahnj@telus.net>
To: Perl Beginners <beginners@perl.org>
Sent: Wednesday, 25 October, 2006 12:11:02 PM
Subject: Re: Fw: Reg:need to find the particular pattern in a line and assign to variables.


pradeep reddy wrote:
> Hello all,


Hello,

> Iam new member to this group and also beginner to PERL.
> Here ismy question,plz let me know your inpus:
> I have a PERL script which gives error report at the end.
> Here is the output.
>
> cleartool: Error: Unable to create label "Pradeep" on "/vob/rootinclude/paer.c" version "/main/3".
> cleartool: Error: Unable to create label "Pradeep" on "/vob/rootinclude/pcme.h" version "/main/2".
>
> I need to grab the two elements between the two quotes.
> Iam very much beginer to the PERL script.
> Iam trying this bit of code, not sure how to go on.
>
> foreach (@problemLines) {
> push $outLine,@problemLines; /"here Iam trying to get the each line into outLine"/


The first argument to push() must be an array so that will not work.


> if ($outLine =~ / cleartool: Error: Unable to create label /) { /"here Iam trying to match the pattern"/
> my @values = split(' " ',$outline); /"Here Iam splitting the outline"/
> my $pathName = @values[3];
> my $Version = @values[5];
> cleartool mklabel $RECURSE $REPLACE -version $Version $labelName $pathName; /"This is the comand Iam using"/
> print "$_\n";
>
> }


You probably want something like this:

my ( $labelName, $pathName, $Version ) = $outline =~ /"([^"]+)"/g;



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>



________________________________________
__________________
Yahoo! India Answers: Share what you know. Learn something new
http://in.answers.yahoo.com/
D. Bolliger

2006-10-30, 7:03 pm

pradeep reddy am Mittwoch, 25. Oktober 2006 09:02:
> John,
>
> Thx for your inputs.
>
> 1st i/p:


Hello

please don't top post so the discussion can be followed easily.

> Iam afraid, I cant read each line from array problemLines to outLine.


You don't show how you modified your code after John's advice.

foreach (@problemLines) { # for all lines do:
# handle the current line in $_
# [no need for the $outLine variable]
}

> 2nd i/p:
>
> =~ /"([^"]+)"/g;------What is the logic in this expression?
> Can u bit ellaborate.
>
> I wanted to read the values in double quotes, from each line.


It does what you want: Extract (by capturing with ()) all (/g) strings ([^"]+)
between two double quotes (/"<snip>"/).

Look at the documentation (from command line): perldoc perlre

> And , Iam using this logic ---if ($outLine =~ / cleartool: Error: Unable to
> create label /)
> , to check for the patten in part of line..Is this correct?


Spaces are relevant. According to your output below, there is no space
before "cleartool".

To check if it's correct, simply try it and look at the result.

Hope this helps!

Dani

[Original question & JWK's 1st answer:]
> ----- Original Message ----
> From: John W. Krahn <krahnj@telus.net>
> To: Perl Beginners <beginners@perl.org>
> Sent: Wednesday, 25 October, 2006 12:11:02 PM
> Subject: Re: Fw: Reg:need to find the particular pattern in a line and
> assign to variables.
>
> pradeep reddy wrote:
>
> Hello,
>
>
> The first argument to push() must be an array so that will not work.
>
>
> You probably want something like this:
>
> my ( $labelName, $pathName, $Version ) = $outline =~ /"([^"]+)"/g;
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you can special-order
> certain sorts of tools at low cost and in short order. -- Larry Wall

Pradeep Reddy

2006-10-30, 7:03 pm

Hello,

This is code I used and it executed in a wanted way.

foreach $outLine(@problemLines) {
if ($outLine =~ "cleartool: Error: Unable to create label" ) {
my @values = split('"',$outline);
my @label = @values[1];
my $pathName = @values[3];
my $Version = @values[5];
cleartool mklabel $RECURSE $REPLACE -version $Version $label $pathName;
}

Anyways, as per Bolliger's i/p I with try to use $_ instead of with outLine varible.

Thx PERL team.

-Pradeep.



----- Original Message ----
From: D. Bolliger <info@dbolliger.ch>
To: beginners@perl..org
Sent: Wednesday, 25 October, 2006 4:36:10 PM
Subject: Re: Fw: Reg:need to find the particular pattern in a line and assign to variables.


pradeep reddy am Mittwoch, 25. Oktober 2006 09:02:
> John,
>
> Thx for your inputs.
>
> 1st i/p:


Hello

please don't top postso the discussion can be followed easily.

> Iam afraid, I cant read each line from array problemLines to outLine.


You don't show how you modified your code after John's advice.

foreach (@problemLines) { # forall lines do:
# handle the current line in $_
# [no need for the $outLine variable]
}

> 2nd i/p:
>
> =~ /"([^"]+)"/g;------What is the logic in this expression?
> Can u bit ellaborate.
>
> I wanted to read the values in double quotes, from each line.


It does what you want: Extract (by capturing with ()) all (/g) strings ([^"]+)
between two double quotes (/"<snip>"/).

Look at the documentation (from command line): perldoc perlre

> And , Iam using this logic ---if ($outLine =~ / cleartool: Error: Unable to
> create label /)
> , to check forthe patten in part of line..Is this correct?


Spaces are relevant. According to your output below, there is no space
before "cleartool".

To check if it's correct, simply try it and look at the result.

Hope this helps!

Dani

[Original question & JWK's 1st answer:]
> -----Original Message ----
> From: John W. Krahn <krahnj@telus.net>
> To: Perl Beginners <beginners@perl.org>
> Sent: Wednesday, 25 October, 2006 12:11:02 PM
> Subject: Re: Fw: Reg:need to find the particular pattern in aline and
> assign to variables.
>
> pradeep reddy wrote:
>
> Hello,
>
>
> The first argument to push() must be an array so that will not work.
>
>
> You probably want something like this:
>
> my ( $labelName, $pathName, $Version ) = $outline =~ /"([^"]+)"/g;
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you can special-order
> certain sorts of tools at low cost and in short order. -- Larry Wall


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn..perl.org/> <http://learn.perl.org/first-response>



________________________________________
__________________
Yahoo! India Answers:Share what you know. Learn something new
http://in.answers.yahoo.com/
D. Bolliger

2006-10-30, 7:03 pm

pradeep reddy am Mittwoch, 25. Oktober 2006 13:52:
> Hello,
>
> This is code I used and it executed in a wanted way.
>
> foreach $outLine(@problemLines) {
> if ($outLine =~ "cleartool: Error: Unable to create label" ) {
> my @values = split('"',$outline);
> my @label = @values[1];
> my $pathName = @values[3];
> my $Version = @values[5];
> cleartool mklabel $RECURSE $REPLACE -version $Version $label $pathName;
> }
>
> Anyways, as per Bolliger's i/p I with try to use $_ instead of with outLine
> varible.


It's not *necessary* to use $_ instead of $outLine, especially not in code
that already does what you want...

But I don't think that above code does what you want (from looking at it). For
example, there is no $label variable defined/assigned.

Put the following standard lines at the top of your script (after the shebang
line):

use strict;
use warnings;

and adjust the code according to the help that will show up :-)

Dani

P.S. Again, *please*, don't top post.

[color=darkred]
> ----- Original Message ----
> From: D. Bolliger <info@dbolliger.ch>
> To: beginners@perl.org
> Sent: Wednesday, 25 October, 2006 4:36:10 PM
> Subject: Re: Fw: Reg:need to find the particular pattern in a line and
> assign to variables.
>
> pradeep reddy am Mittwoch, 25. Oktober 2006 09:02:
>
> Hello
>
> please don't top post so the discussion can be followed easily.
>
>
> You don't show how you modified your code after John's advice.
>
> foreach (@problemLines) { # for all lines do:
> # handle the current line in $_
> # [no need for the $outLine variable]
> }
>
>
> It does what you want: Extract (by capturing with ()) all (/g) strings
> ([^"]+) between two double quotes (/"<snip>"/).
>
> Look at the documentation (from command line): perldoc perlre
>
>
> Spaces are relevant. According to your output below, there is no space
> before "cleartool".
>
> To check if it's correct, simply try it and look at the result.
>
> Hope this helps!
>
> Dani
>
> [Original question & JWK's 1st answer:]
>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com