problem with control structures ( if and next)

V

varala_kanth

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
 
J

Jürgen Exner

varala_kanth said:
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
 
J

Joe Smith

varala_kanth said:
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.
 
V

varala_kanth

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 said:
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/attachment.php?postid=708305 |
+----------------------------------------------------------------+
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top