problem with control structures ( if and next)

Discussion in 'Perl' started by varala_kanth, Jun 19, 2004.

  1. varala_kanth

    varala_kanth Guest

    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
    ------------------------------------------------------------------------
     
    varala_kanth, Jun 19, 2004
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Jun 19, 2004
    #2
    1. Advertising

  3. varala_kanth

    Joe Smith Guest

    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.
     
    Joe Smith, Jun 20, 2004
    #3
  4. varala_kanth

    varala_kanth Guest

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


    --
    varala_kanth
    ------------------------------------------------------------------------
    Posted via http://www.codecomments.com
    ------------------------------------------------------------------------
     
    varala_kanth, Jun 20, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,820
    Eric Sosman
    Jun 11, 2004
  2. Deniz Bahar
    Replies:
    2
    Views:
    518
    Andrey Tarasevich
    Mar 9, 2005
  3. Alfonso Morra
    Replies:
    11
    Views:
    755
    Emmanuel Delahaye
    Sep 24, 2005
  4. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    827
    Alf P. Steinbach
    Jul 10, 2007
  5. Tad McClellan
    Replies:
    3
    Views:
    185
    Edward Wijaya
    May 13, 2004
Loading...

Share This Page