Re: I need help here

2005-10-14 Thread Chris Devers

On Fri, 14 Oct 2005, Sreedhar reddy wrote:

I am very new to PERL and I need a program to do following task. Can 
[you] help me [please][question-mark]



I have a file with following contents.

454 NV_DS_DEFAULT_BAUDRATE_I
455 NV_DIAG_DEFAULT_BAUDRATE_I
516 NV_WCDMA_RX_LIN_VS_TEMP_I

I am expecting out put like

NV_DS_DEFAULT_BAUDRATE_I 454
NV_DIAG_DEFAULT_BAUDRATE_I 455
NV_WCDMA_RX_LIN_VS_TEMP_I 516

Basically it is interchanging of columns. Looking forward for your help.


Yes, this is possible.

Have you tried anything so far?

You sent your message to beginners@perl.org and beginners-cgi@perl.org, 
but not [EMAIL PROTECTED] On the lists you 
wrote to, you need to take the effort to write at least some code, which 
we can then help you with.


Here's a hint, but it's intentionally incomplete:

  sub swap_columns {
my ( $first, $second ) = @_;
return ( $second, $first );
  }

You should be able to apply that to each line of input to generate each 
line of output. There's lots of other ways to go about this as well 
though, and if you provide some code we can help you work through it.



--
Chris Devers

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: I need help here

2005-10-14 Thread Sreedhar reddy
Hi Chris...

I tried like this...

INPUT = fopen("filename");
OUTPUT   -- I opend another file here to write output

while() {
chomp @_;
@strings = split(@_, " ");
print OUTPUT strings[1] + strings[0];
}


But I got error msg...Can u modify this program if it is correct???

-Original Message-
From: Chris Devers [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 14, 2005 6:06 PM
To: Sreedhar reddy
Cc: beginners@perl.org
Subject: Re: I need help here

On Fri, 14 Oct 2005, Sreedhar reddy wrote:

> I am very new to PERL and I need a program to do following task. Can 
> [you] help me [please][question-mark]
>
>
> I have a file with following contents.
>
> 454 NV_DS_DEFAULT_BAUDRATE_I
> 455 NV_DIAG_DEFAULT_BAUDRATE_I
> 516 NV_WCDMA_RX_LIN_VS_TEMP_I
>
> I am expecting out put like
>
> NV_DS_DEFAULT_BAUDRATE_I 454
> NV_DIAG_DEFAULT_BAUDRATE_I 455
> NV_WCDMA_RX_LIN_VS_TEMP_I 516
>
> Basically it is interchanging of columns. Looking forward for your help.

Yes, this is possible.

Have you tried anything so far?

You sent your message to beginners@perl.org and beginners-cgi@perl.org, 
but not [EMAIL PROTECTED] On the lists you 
wrote to, you need to take the effort to write at least some code, which 
we can then help you with.

Here's a hint, but it's intentionally incomplete:

   sub swap_columns {
 my ( $first, $second ) = @_;
 return ( $second, $first );
   }

You should be able to apply that to each line of input to generate each 
line of output. There's lots of other ways to go about this as well 
though, and if you provide some code we can help you work through it.


-- 
Chris Devers



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: I need help here

2005-10-14 Thread Sreedhar reddy
Chris-
Can u suggest good links to gain good knowledge in PERL...


Thanks,
Sridhar Reddy

-Original Message-
From: Chris Devers [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 14, 2005 6:06 PM
To: Sreedhar reddy
Cc: beginners@perl.org
Subject: Re: I need help here

On Fri, 14 Oct 2005, Sreedhar reddy wrote:

> I am very new to PERL and I need a program to do following task. Can 
> [you] help me [please][question-mark]
>
>
> I have a file with following contents.
>
> 454 NV_DS_DEFAULT_BAUDRATE_I
> 455 NV_DIAG_DEFAULT_BAUDRATE_I
> 516 NV_WCDMA_RX_LIN_VS_TEMP_I
>
> I am expecting out put like
>
> NV_DS_DEFAULT_BAUDRATE_I 454
> NV_DIAG_DEFAULT_BAUDRATE_I 455
> NV_WCDMA_RX_LIN_VS_TEMP_I 516
>
> Basically it is interchanging of columns. Looking forward for your help.

Yes, this is possible.

Have you tried anything so far?

You sent your message to beginners@perl.org and beginners-cgi@perl.org, 
but not [EMAIL PROTECTED] On the lists you 
wrote to, you need to take the effort to write at least some code, which 
we can then help you with.

Here's a hint, but it's intentionally incomplete:

   sub swap_columns {
 my ( $first, $second ) = @_;
 return ( $second, $first );
   }

You should be able to apply that to each line of input to generate each 
line of output. There's lots of other ways to go about this as well 
though, and if you provide some code we can help you work through it.


-- 
Chris Devers



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: I need help here

2005-10-14 Thread Chris Devers
You accidentally replied just to me, not to the list. Please don't do 
that. Send all messages to the list and I will read them there, along 
with all the other helpful list subscribers.


On Fri, 14 Oct 2005, Sreedhar reddy wrote:


I tried like this...

INPUT = fopen("filename");
OUTPUT   -- I opened another file here to write output

while() {
chomp @_;
@strings = split(@_, " ");
print OUTPUT strings[1] + strings[0];
}


But I got error [message]...Can [you] modify this program if it is 
correct???


If it's correct, it doesn't need to be modified, does it? :-)

If you want help with your errors, you need to *tell us what the error 
message was*!


Is the text above literally what you wrote? If so, it can't work -- 
INPUT isn't a valid variable name (it needs to have a 'sigil', like $, 
@, or %) and the "OUTPUT -- I..." line just looks like a comment. I 
assume that isn't really your code, is it?


Please show the list exactly what you're doing and what's happening when 
you try to do it: what the exact statements are to declare your input 
and output files are, what the while{} loop is (this appears complete, 
but I want to be sure), and what the error message was.




--
Chris Devers

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: I need help here

2005-10-14 Thread John W. Krahn
Sreedhar reddy wrote:
> Hi,

Hello,

> I am very new to PERL  and I need a program to do following task. Can u help
> me pls.
> 
> 
> I have a file with following contents.
> 
> 454 NV_DS_DEFAULT_BAUDRATE_I
> 455 NV_DIAG_DEFAULT_BAUDRATE_I
> 516 NV_WCDMA_RX_LIN_VS_TEMP_I
> 
> I am expecting out put like 
> 
> NV_DS_DEFAULT_BAUDRATE_I 454
> NV_DIAG_DEFAULT_BAUDRATE_I 455
> NV_WCDMA_RX_LIN_VS_TEMP_I 516
> 
> Basically it is interchanging of columns. Looking forward for your help.

perl -i.bak -lane'print join " ", reverse @F' yourfile



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: I need help here

2005-10-14 Thread Sreedhar reddy
> INPUT = fopen("filename");
> OUTPUT   -- I opened another file here to write output


These two lines r just to say how I'm proceeding...

The err msg which I got is...

"Can't modify constant item in scalar assignment at test_script.pl line 1,
near "

);"

syntax error at test_script.pl line 13, near "strings["

Execution of test_script.pl aborted due to compilation errors."


Can u tell me wht is meant by this ?

Thanks,
Sridhar

-Original Message-
From: Chris Devers [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 14, 2005 6:59 PM
To: Sreedhar reddy
Cc: beginners@perl.org
Subject: RE: I need help here

You accidentally replied just to me, not to the list. Please don't do 
that. Send all messages to the list and I will read them there, along 
with all the other helpful list subscribers.

On Fri, 14 Oct 2005, Sreedhar reddy wrote:

> I tried like this...
>
> INPUT = fopen("filename");
> OUTPUT   -- I opened another file here to write output
>
> while() {
> chomp @_;
> @strings = split(@_, " ");
> print OUTPUT strings[1] + strings[0];
> }
>
>
> But I got error [message]...Can [you] modify this program if it is 
> correct???

If it's correct, it doesn't need to be modified, does it? :-)

If you want help with your errors, you need to *tell us what the error 
message was*!

Is the text above literally what you wrote? If so, it can't work -- 
INPUT isn't a valid variable name (it needs to have a 'sigil', like $, 
@, or %) and the "OUTPUT -- I..." line just looks like a comment. I 
assume that isn't really your code, is it?

Please show the list exactly what you're doing and what's happening when 
you try to do it: what the exact statements are to declare your input 
and output files are, what the while{} loop is (this appears complete, 
but I want to be sure), and what the error message was.



-- 
Chris Devers



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: I need help here

2005-10-14 Thread Sreedhar reddy
Finally I finished this program like this...




$inputfile= shift;
$outputfile = shift;
open(FILE1,"<$inputfile");
@lines=;
close(FILE1);
open(FILE2,">$outputfile");

foreach $line (@lines) {
if($line =~ m/(\d+) ([a-z,A-Z,_,0-9]+)/) /*  to write
this line I have taken lot of time as I know basics in Regular Expressions
*/
{
print FILE2 "$2 $1\n";
}
} 
close(FILE2);
print "\n"


Thanks to all who helped me to solve this problem...

-Original Message-
From: Chris Devers [mailto:[EMAIL PROTECTED] 
Sent: Friday, October 14, 2005 6:59 PM
To: Sreedhar reddy
Cc: beginners@perl.org
Subject: RE: I need help here

You accidentally replied just to me, not to the list. Please don't do 
that. Send all messages to the list and I will read them there, along 
with all the other helpful list subscribers.

On Fri, 14 Oct 2005, Sreedhar reddy wrote:

> I tried like this...
>
> INPUT = fopen("filename");
> OUTPUT   -- I opened another file here to write output
>
> while() {
> chomp @_;
> @strings = split(@_, " ");
> print OUTPUT strings[1] + strings[0];
> }
>
>
> But I got error [message]...Can [you] modify this program if it is 
> correct???

If it's correct, it doesn't need to be modified, does it? :-)

If you want help with your errors, you need to *tell us what the error 
message was*!

Is the text above literally what you wrote? If so, it can't work -- 
INPUT isn't a valid variable name (it needs to have a 'sigil', like $, 
@, or %) and the "OUTPUT -- I..." line just looks like a comment. I 
assume that isn't really your code, is it?

Please show the list exactly what you're doing and what's happening when 
you try to do it: what the exact statements are to declare your input 
and output files are, what the while{} loop is (this appears complete, 
but I want to be sure), and what the error message was.



-- 
Chris Devers



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: I need help here

2005-10-14 Thread Charles K. Clarkson
Sreedhar reddy  wrote:
: Finally I finished this program like this...

[snip]

A working perl script should not be your only concern
when just learning the language. It is equally, no, more
important that you learn how to write your code well. Show
us your entire working script. I think you are using some
very poor techniques, but we need to see everything to
see just what help you need.

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: I need help here

2005-10-14 Thread John W. Krahn
Sreedhar reddy wrote:
> Finally I finished this program like this...

You should have these two lines at the beginning of your program:

use warnings;
use strict;

as they will help you find some mistakes and enforce better coding practices.


> $inputfile= shift;
> $outputfile = shift;
>   open(FILE1,"<$inputfile");

You should *ALWAYS* verify that the file was opened coreectly.

open FILE1, '<', $inputfile or die "Cannot open $inputfile: $!";


>   @lines=;

There is no good reason to read all the lines into memory as you only need to
process one line at a time.


>   close(FILE1);
>   open(FILE2,">$outputfile");

You should *ALWAYS* verify that the file was opened coreectly.

open FILE2, '>', $outputfile or die "Cannot open $outputfile: $!";


> foreach $line (@lines) {
>   if($line =~ m/(\d+) ([a-z,A-Z,_,0-9]+)/) /*  to write

Perl provides the escape sequence \w that represents the character class
[a-zA-Z0-9_] so your regular expression could be written /(\d+) ([\w,]+)/
although I didn't see any commas in your data so you probably really want
/(\d+) (\w+)/.

Also perl doesn't understand /* C style comments */.


> this line I have taken lot of time as I know basics in Regular Expressions
> */
>   {
>   print FILE2 "$2 $1\n";
>   }
> }
> close(FILE2);
> print "\n"


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: I need help here

2005-10-14 Thread Sreedhar reddy
Thanks a Lot John...

I appreciate your feedback which helps me to improve myself...

Cheers.
Sridhar Reddy

-Original Message-
From: John W. Krahn [mailto:[EMAIL PROTECTED] 
Sent: Saturday, October 15, 2005 11:05 AM
To: Perl Beginners
Subject: Re: I need help here

Sreedhar reddy wrote:
> Finally I finished this program like this...

You should have these two lines at the beginning of your program:

use warnings;
use strict;

as they will help you find some mistakes and enforce better coding
practices.


> $inputfile= shift;
> $outputfile = shift;
>   open(FILE1,"<$inputfile");

You should *ALWAYS* verify that the file was opened coreectly.

open FILE1, '<', $inputfile or die "Cannot open $inputfile: $!";


>   @lines=;

There is no good reason to read all the lines into memory as you only need
to
process one line at a time.


>   close(FILE1);
>   open(FILE2,">$outputfile");

You should *ALWAYS* verify that the file was opened coreectly.

open FILE2, '>', $outputfile or die "Cannot open $outputfile: $!";


> foreach $line (@lines) {
>   if($line =~ m/(\d+) ([a-z,A-Z,_,0-9]+)/) /*  to write

Perl provides the escape sequence \w that represents the character class
[a-zA-Z0-9_] so your regular expression could be written /(\d+) ([\w,]+)/
although I didn't see any commas in your data so you probably really want
/(\d+) (\w+)/.

Also perl doesn't understand /* C style comments */.


> this line I have taken lot of time as I know basics in Regular Expressions
> */
>   {
>   print FILE2 "$2 $1\n";
>   }
> }
> close(FILE2);
> print "\n"


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: I need help here

2005-10-15 Thread Charles K. Clarkson
Sreedhar reddy  wrote:

: Charles <> wrote:

: : A working perl script should not be your only concern
: : when just learning the language. It is equally, no, more
: : important that you learn how to write your code well. Show
: : us your entire working script. I think you are using some
: : very poor techniques, but we need to see everything to
: : see just what help you need.
:
:   Definately I need the help of well experienced people like
: you moving forward..
:
: Can you suggest me some good links to learn PERL in depth

You don't need another link or source. Show us the complete
code for your solution and HUMAN BEINGS from across the world will
review it and tell you where you need improvement. No web site can
do that for you.

Come on, cough up the code. It will only hurt a little.


Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: I need help here

2005-10-15 Thread Sreedhar reddy
Hi Charles,


This is the code which I have written to finish my small task...


$inputfile= shift;
$outputfile = shift;
open(FILE1,"<$inputfile");
@lines=;
close(FILE1);
open(FILE2,">$outputfile");

foreach $line (@lines) {
if($line =~ m/(\d+) ([a-z,A-Z,_,0-9]+)/) /*  to write
this line I have taken lot of time as I know basics in Regular Expressions
*/
{
print FILE2 "$2 $1\n";
}
}
close(FILE2);
print "\n"



In attached mail you can find some comments from john also. If you have any
comments, pls let me know...



-Original Message-
From: Charles K. Clarkson [mailto:[EMAIL PROTECTED] 
Sent: Saturday, October 15, 2005 4:27 PM
To: 'Perl Beginners'
Subject: RE: I need help here

Sreedhar reddy <mailto:[EMAIL PROTECTED]> wrote:

: Charles <> wrote:

: : A working perl script should not be your only concern
: : when just learning the language. It is equally, no, more
: : important that you learn how to write your code well. Show
: : us your entire working script. I think you are using some
: : very poor techniques, but we need to see everything to
: : see just what help you need.
:
:   Definately I need the help of well experienced people like
: you moving forward..
:
: Can you suggest me some good links to learn PERL in depth

You don't need another link or source. Show us the complete
code for your solution and HUMAN BEINGS from across the world will
review it and tell you where you need improvement. No web site can
do that for you.

Come on, cough up the code. It will only hurt a little.


Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


--- Begin Message ---
Title: RE: I need help here






Thanks a Lot John...


I appreciate your feedback which helps me to improve myself...


Cheers.

Sridhar Reddy


-Original Message-

From: John W. Krahn [mailto:[EMAIL PROTECTED]] 

Sent: Saturday, October 15, 2005 11:05 AM

To: Perl Beginners

Subject: Re: I need help here


Sreedhar reddy wrote:

> Finally I finished this program like this...


You should have these two lines at the beginning of your program:


use warnings;

use strict;


as they will help you find some mistakes and enforce better coding

practices.



> $inputfile= shift;

> $outputfile = shift;

>   open(FILE1,"<$inputfile");


You should *ALWAYS* verify that the file was opened coreectly.


open FILE1, '<', $inputfile or die "Cannot open $inputfile: $!";



>   @lines=;


There is no good reason to read all the lines into memory as you only need

to

process one line at a time.



>   close(FILE1);

>   open(FILE2,">$outputfile");


You should *ALWAYS* verify that the file was opened coreectly.


open FILE2, '>', $outputfile or die "Cannot open $outputfile: $!";



> foreach $line (@lines) {

>   if($line =~ m/(\d+) ([a-z,A-Z,_,0-9]+)/) /*  to write


Perl provides the escape sequence \w that represents the character class

[a-zA-Z0-9_] so your regular _expression_ could be written /(\d+) ([\w,]+)/

although I didn't see any commas in your data so you probably really want

/(\d+) (\w+)/.


Also perl doesn't understand /* C style comments */.



> this line I have taken lot of time as I know basics in Regular Expressions

> */

>   {

>           print FILE2 "$2 $1\n";

>   }

> }

> close(FILE2);

> print "\n"



John

-- 

use Perl;

program

fulfillment


-- 

To unsubscribe, e-mail: [EMAIL PROTECTED]

For additional commands, e-mail: [EMAIL PROTECTED]

<http://learn.perl.org/> <http://learn.perl.org/first-response>





-- 

To unsubscribe, e-mail: [EMAIL PROTECTED]

For additional commands, e-mail: [EMAIL PROTECTED]

<http://learn.perl.org/> <http://learn.perl.org/first-response>




--- End Message ---
-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


RE: I need help here

2005-10-16 Thread Charles K. Clarkson
Sreedhar reddy  wrote:
: Hi Charles,
:
:
: This is the code which I have written to finish my small task...

Oh. So I actually had the whole thing to begin with. Sorry
about that.


Your script is using a different set of variables than the
ones we would like to see you use. Mark J Dominus wrote an article
for The Perl Journal in 1998. The full article is "reprinted" at
http://perl.plover.com/FAQs/Namespaces.html



 Here's the Big Secret about Perl variables that most people learn
 too late: Perl has two completely separate, independent sets of
 variables. One is left over from Perl 4, and the other is new.
 The two sets of variables are called 'package variables' and
 'lexical variables', and they have nothing to do with each other.



While this was a secret in 1998 and while it is true that some
perl programmers still choose package variables over lexical
variables, we, the perl community, would like to follow Mark's
warning and introduce beginners to lexical variables from the
start.

Your script should start with these modules. They help you
by discouraging some actions. They also catch many typos. And
they limit you from accidentally using package variables.

use strict;
use warnings;

You might also add this module. It will give you a better idea
about how to solve errors (and even where to go for research).

use diagnostics;

So, for a while your scripts should start like this.

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;



: $inputfile= shift;
: $outputfile = shift;

To change these package variables to lexical variables, we
need only use the 'my' function. I also like to line up equality
signs when writing code and to separate words in a variable name
with an underscores and, like you, everything is in lowercase.

my $input_file  = shift;
my $output_file = shift;


You will often see a usage message in a script which deals
with the command line. It's good to use it for your own utilities
as well. Eight months from now you may have forgotten what belongs
on the command line or what this utility does. usage() is a
subroutine in the script which tells users what the script does
and what is expected on the commend line.

usage() if @ARGV < 2;

my $input_file  = shift;
my $output_file = shift;

# or:

usage() if @ARGV < 2;

my( $input_file, $output_file ) = @ARGV;

When perl evaluates the Boolean "@ARGV < 2" it uses the scalar
value of @ARGV which is the count of its elements. We want at
least 2 items in there.

sub usage {
print qq(Usage:\n\n$0 INPUT_FILE OUTPUT_FILE\n\n);
print qq(This utility will . . . );
exit;
}

You might take the time to make your usage statements match
the style used by your operating system. Then users can quickly
grasp what your script does and how to use it. ($0 is a built in
perl variable. You can read more about these variables in the
'perlvar' file of the perl documentation.)



:   open(FILE1,"<$inputfile");
:   @lines=;
:   close(FILE1);
:   open(FILE2,">$outputfile");

Even with strict variables enforced, we still tend to use
package variables in some traditional ways. FILE1 and FILE2 are
file handles. Or perhaps they are references to file handles. I'm
not really sure. But they are package variables.

Almost all programs you run across use this style and strict
allows it, but I don't like it. Perl 5.6.0 allowed us to stop
using these in favor of lexical variables.

open( my $fh, "<$input_file" );



 If FILEHANDLE is an undefined scalar variable (or array or hash
 element) the variable is assigned a reference to a new anonymous
 filehandle, otherwise if FILEHANDLE is an expression, its value
 is used as the name of the real filehandle wanted. (This is
 considered a symbolic reference, so "use strict 'refs'" should
 *not* be in effect.)



While this is a better way, it hasn't caught on yet. So please
don't feel confused when you see some of us use FH and FILE
and others use $fh and $fh_in (lexical variables).

Not being a big fan of excess parenthesis, I would rewrite
your statement as below. I am using the three argument form for
open. You can read more about it in "perlopentut" in the perl
docs. It will also explain why I am checking for success on the
file open and the file close. (Many people don't check the closing
for an error.)

I tend to be lean on variable use. Reusing variables when
it doesn't hurt readability. YMMV. So I just reused $fh as my file
handle for the output file.


open my $fh, '<', $input_file or die qq(Cannot open "$input_file": $!);

my @lines = <$fh>;

close $fh or die qq(Cannot close "$input_file": $!);

open $fh, '>', $output_file or die qq(Cannot open "$output_file": $!);



: foreach $line (@lines) {

$line is a package variable. Let's make it lexical.

foreach my $line (@lines) {


:   if($line =~ m/(\d+) ([a-z,A-Z,_,0-9]+)/) /*  to write
: this line I have taken lot of time as I know basics in Regular

RE: I need help here

2005-10-16 Thread Sreedhar reddy
Hi Charles,
I am really thankful for your detailed explanation. I will try to
gain as much as I can from this mail.

With Regards,
Sridhar Reddy T.

-Original Message-
From: Charles K. Clarkson [mailto:[EMAIL PROTECTED] 
Sent: Monday, October 17, 2005 12:15 AM
To: 'Perl Beginners'
Subject: RE: I need help here

Sreedhar reddy <mailto:[EMAIL PROTECTED]> wrote:
: Hi Charles,
:
:
: This is the code which I have written to finish my small task...

Oh. So I actually had the whole thing to begin with. Sorry
about that.


Your script is using a different set of variables than the
ones we would like to see you use. Mark J Dominus wrote an article
for The Perl Journal in 1998. The full article is "reprinted" at
http://perl.plover.com/FAQs/Namespaces.html



 Here's the Big Secret about Perl variables that most people learn
 too late: Perl has two completely separate, independent sets of
 variables. One is left over from Perl 4, and the other is new.
 The two sets of variables are called 'package variables' and
 'lexical variables', and they have nothing to do with each other.



While this was a secret in 1998 and while it is true that some
perl programmers still choose package variables over lexical
variables, we, the perl community, would like to follow Mark's
warning and introduce beginners to lexical variables from the
start.

Your script should start with these modules. They help you
by discouraging some actions. They also catch many typos. And
they limit you from accidentally using package variables.

use strict;
use warnings;

You might also add this module. It will give you a better idea
about how to solve errors (and even where to go for research).

use diagnostics;

So, for a while your scripts should start like this.

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;



: $inputfile= shift;
: $outputfile = shift;

To change these package variables to lexical variables, we
need only use the 'my' function. I also like to line up equality
signs when writing code and to separate words in a variable name
with an underscores and, like you, everything is in lowercase.

my $input_file  = shift;
my $output_file = shift;


You will often see a usage message in a script which deals
with the command line. It's good to use it for your own utilities
as well. Eight months from now you may have forgotten what belongs
on the command line or what this utility does. usage() is a
subroutine in the script which tells users what the script does
and what is expected on the commend line.

usage() if @ARGV < 2;

my $input_file  = shift;
my $output_file = shift;

# or:

usage() if @ARGV < 2;

my( $input_file, $output_file ) = @ARGV;

When perl evaluates the Boolean "@ARGV < 2" it uses the scalar
value of @ARGV which is the count of its elements. We want at
least 2 items in there.

sub usage {
print qq(Usage:\n\n$0 INPUT_FILE OUTPUT_FILE\n\n);
print qq(This utility will . . . );
exit;
}

You might take the time to make your usage statements match
the style used by your operating system. Then users can quickly
grasp what your script does and how to use it. ($0 is a built in
perl variable. You can read more about these variables in the
'perlvar' file of the perl documentation.)



:   open(FILE1,"<$inputfile");
:   @lines=;
:   close(FILE1);
:   open(FILE2,">$outputfile");

Even with strict variables enforced, we still tend to use
package variables in some traditional ways. FILE1 and FILE2 are
file handles. Or perhaps they are references to file handles. I'm
not really sure. But they are package variables.

Almost all programs you run across use this style and strict
allows it, but I don't like it. Perl 5.6.0 allowed us to stop
using these in favor of lexical variables.

open( my $fh, "<$input_file" );



 If FILEHANDLE is an undefined scalar variable (or array or hash
 element) the variable is assigned a reference to a new anonymous
 filehandle, otherwise if FILEHANDLE is an expression, its value
 is used as the name of the real filehandle wanted. (This is
 considered a symbolic reference, so "use strict 'refs'" should
 *not* be in effect.)



While this is a better way, it hasn't caught on yet. So please
don't feel confused when you see some of us use FH and FILE
and others use $fh and $fh_in (lexical variables).

Not being a big fan of excess parenthesis, I would rewrite
your statement as below. I am using the three argument form for
open. You can read more about it in "perlopentut" in the perl
docs. It will also explain why I am checking for success on the
file open and the file close. (Many people don't check the closing
for an error.)

I tend to be lean on variable use. Reusing variables when
it doesn't hurt readability. YMMV. S

RE: I need help here

2005-10-17 Thread Charles K. Clarkson
Bowen, Bruce  wrote:

: If I "declare" my 'lexical variables' at the beginning of a
: file, and then refer to the variable without the 'my ' in front
: of it, does that change it back to a package variable or does
: that create a new package variable?

Once a variable is declared using 'my' it can never be also be
a package variable. They can refer to each other in such a way that
changing one seems to change the other, but they are not the same
type of variable.

Think of the two types of perl variables as an apple orchard
and peach orchard. If we grow an apple tree it can never become
a peach tree and there are no peachapple trees. :)


In no case can you use 'my' on the same variable in the same
scope. Well, you can, but the warnings module will throw a warning
about masking an earlier declaration. If your not sure what scope
is, read the article I mentioned earlier.

http://perl.plover.com/FAQs/Namespaces.html


Now, you should try to keep your variables in the smallest
scope possible. That means do not declare all of them at the top of
your script. In some programming languages this is encouraged. Not
in perl. Trust me and don't do it like that.

Having said that, there are sometimes reasons to place a
variable at the top of a script. Configuration variables come to
mind. While developing it is nice to have a configuration hash at
the top of the script. In production, we would probably add a
configuration or serialization module and place that information
in another file.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]