Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > problem with control structures ( if and next)

Reply
Thread Tools

problem with control structures ( if and next)

 
 
varala_kanth
Guest
Posts: n/a
 
      06-19-2004

hello friends,

iam completely new bie iam tracing one

perl program .

this part of the code

</code>
sub parseRadAcct {

my ($refHashParam) = @_;
my $fname = $refHashParam->{'opt'}->{'radacct'};
open(RADACCT, "<$RADDIR/$fname")
my $l = "";
my $nr = 0;
my $ln = 0;
my %hashRec = ();
my $i = 0;
my $err = 0;
my $recLine = 0;
while($l = <RADACCT> ) {
$ln++;
chomp $l;

# look for start of record
if ($l =~ /^\w+\s+\w+\s+\d+\s+\d+:\d+:\d+\s+\d+$/m) {
$nr = 1;
%hashRec = ();
$recLine = $ln;
next;
};
printf ("hello");



};
</code>

my doubts are its not printing hello why?
and
what am i checking with if condition?

and what is storing in %hashRec


kindly help me

with regards
rama kanth



--
varala_kanth
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-19-2004
varala_kanth wrote:
> iam completely new bie iam tracing one
> perl program .


[Your indention style is horrible; I strongly suggest you improve that, it
would make your code much more readable]

> sub parseRadAcct {
>
> my ($refHashParam) = @_;
> my $fname = $refHashParam->{'opt'}->{'radacct'};
> open(RADACCT, "<$RADDIR/$fname")


This doesn't even compile! You are missing a semicolon.
Plus a test if the open succeded.

> my $l = "";
> my $nr = 0;
> my $ln = 0;
> my %hashRec = ();
> my $i = 0;
> my $err = 0;
> my $recLine = 0;


No need for any of those initializations. Those are all the default values
anyway.

> while($l = <RADACCT> ) {


Is this a $1 (one) or a $l (lima)?
I suggest you don't use characters that can easily be confused as variable
names. l (lima) is one of them, the other hot candidate is o (oscar).
And why using an explicit name anyway? The default $_ ought to work just as
well and wouldn't cause a lot of head scratching as to why the programmer
decided to introduce an additional variable.

> $ln++;
> chomp $l;
>
> # look for start of record
> if ($l =~ /^\w+\s+\w+\s+\d+\s+\d+:\d+:\d+\s+\d+$/m) {
> $nr = 1;
> %hashRec = ();
> $recLine = $ln;
> next;
> };
> printf ("hello");


Don't use a printf() when a plain print() will do. See The Fine Manual for
details.

> };
> </code>
>
> my doubts are its not printing hello why?


Some possible reasons:
- You don't call the sub at all
- How do you know that the open() succeeded? You don't check the return
value.
- Your file doesn't contain any data, therefore the while() fails right
away.

> and
> what am i checking with if condition?


You wrote the code, don't you know? Tell us what _you_ had in mind when you
wrote this RE, then we can check if it does what you think it should do.
Reverse engineering REs without any hint to their intended use is a rather
difficult job.

> and what is storing in %hashRec


The hash is repeatedly set to the empty hash, nothing else.

jue


 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      06-20-2004
varala_kanth wrote:

> i try to learn in this way


That is a bad idea. You should learn Perl properly.
Try http://learn.perl.org for information.

> yes open is succeded


Are you sure about that? You should *always* test the value
returned by open() to make sure it actually did succeed.

> i actually have the semicolon
> in my program while copieng it may have gone.


That is why you should copy-and-paste, not re-type the code.

> i kept printf for my understandig purpose


You should use print() instead of printf() for that.

> so please guide me


The code you posted could not possibly work.
It looks like you left something out.
It may help if you posted the actual code.
-Joe

P.S. Post to comp.lang.perl.misc next time; comp.lang.perl is defunct.
 
Reply With Quote
 
varala_kanth
Guest
Posts: n/a
 
      06-20-2004

Hello jue,

thanks first for seeing the code

as i told you iam traceing the code

which was written by others

as i donot have much depth with perl

i try to learn in this way

soory for indentation i will keep it to

the standards next time onwards.

yes open is succeded

and also it has the data in that file

i actually have the semicolon

in my program while copieng it may have

gone.


i have the doubts in the statement like

these

if ($l =~ /^\w+\s+\w+\s+\d+\s+\d+:\d+:\d+\s+\d+$/m)

and after this if condtion it has to

goto(then only that code works properly)

next if i.e ( last record comment code)

iam attaching the program with this

i kept printf for my understandig

purpose


so please guide me



with regards,

rama kanth



OTE]-Originally posted by Jürgen Exner -
*varala_kanth wrote:
> iam completely new bie iam tracing one
> perl program .


[Your indention style is horrible; I strongly suggest you improve that,
it
would make your code much more readable]

> sub parseRadAcct {
>
> my ($refHashParam) = @_;
> my $fname = $refHashParam->{'opt'}->{'radacct'};
> open(RADACCT, "<$RADDIR/$fname")


This doesn't even compile! You are missing a semicolon.
Plus a test if the open succeded.

> my $l = "";
> my $nr = 0;
> my $ln = 0;
> my %hashRec = ();
> my $i = 0;
> my $err = 0;
> my $recLine = 0;


No need for any of those initializations. Those are all the default
values
anyway.

> while($l = <RADACCT> ) {


Is this a $1 (one) or a $l (lima)?
I suggest you don't use characters that can easily be confused as
variable
names. l (lima) is one of them, the other hot candidate is o (oscar).
And why using an explicit name anyway? The default $_ ought to work
just as
well and wouldn't cause a lot of head scratching as to why the
programmer
decided to introduce an additional variable.

> $ln++;
> chomp $l;
>
> # look for start of record
> if ($l =~ /^\w+\s+\w+\s+\d+\s+\d+:\d+:\d+\s+\d+$/m) {
> $nr = 1;
> %hashRec = ();
> $recLine = $ln;
> next;
> };
> printf ("hello");


Don't use a printf() when a plain print() will do. See The Fine Manual
for
details.

> };
> </code>
>
> my doubts are its not printing hello why?


Some possible reasons:
- You don't call the sub at all
- How do you know that the open() succeeded? You don't check the
return
value.
- Your file doesn't contain any data, therefore the while() fails
right
away.

> and
> what am i checking with if condition?


You wrote the code, don't you know? Tell us what _you_ had in mind when
you
wrote this RE, then we can check if it does what you think it should
do.
Reverse engineering REs without any hint to their intended use is a
rather
difficult job.

> and what is storing in %hashRec


The hash is repeatedly set to the empty hash, nothing else.

jue *


+----------------------------------------------------------------+
| Attachment filename: rad2db.txt |
|Download attachment: http://www.codecomments.com/attachme...?postid=708305 |
+----------------------------------------------------------------+


--
varala_kanth
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Control Structures Trent Jones Ruby 11 12-06-2008 08:28 AM
How to jump out of several nested control structures? Shawn Java 13 10-21-2006 11:39 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
newbie having trouble with control structures: loop till recieve string QUIT M. Duijkers Perl 2 12-12-2004 11:02 AM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57