Perl Division Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • youjay
    New Member
    • Jul 2007
    • 1

    Perl Division Problem

    I've been out of perl for a while, so I am starting from scratch. I have a small applet which scans a set of directories, getting information from some files in each one, and displaying selected data.

    The script works good, but I need to set in some tabbing on the terminal, and that is where the problem comes in. as soon as I attempt a division, the applet blows up, and it is not a divide by 0 error.

    I have tested the particular line of code with all arithmetic operators, and the code only refuses to compile when I use the division operator.

    The error display is:

    Bareword found where operator expected at ./ideaStatus line 51, near "case"
    (Missing semicolon on previous line?)
    Bareword found where operator expected at ./ideaStatus line 52, near "case"
    (Missing semicolon on previous line?)
    Bareword found where operator expected at ./ideaStatus line 53, near "case"
    (Missing semicolon on previous line?)
    syntax error at ./ideaStatus line 49, near ") {"
    syntax error at ./ideaStatus line 51, near "/^"
    syntax error at ./ideaStatus line 52, near "/^"
    syntax error at ./ideaStatus line 53, near "/^"
    syntax error at ./ideaStatus line 53, near "; }"
    Execution of ./ideaStatus aborted due to compilation errors.

    The code is:

    [CODE=perl]
    #!/usr/bin/perl
    use Switch;
    use integer;


    $baseDir = "~/Desktop/IdeaBin";
    $tablen = 8;

    #-----------
    # list existing project ideas

    print "locating existing project ideas from $baseDir\n";
    $listIdx = 0;

    opendir(DIRHAND LE, $baseDir) || die "Cannot opendir $baseDir: $!";
    foreach $name (sort readdir(DIRHAND LE)) {
    # --- make sure it is a directory
    if (-d "$baseDir/$name") {
    # --- and it's not the . .. dir links
    if ( $name !~ /^\./ ) {
    @Ideas[$listIdx]=$name;
    $listIdx++;
    }
    }
    }
    closedir(DIRHAN DLE);


    print "Code\t\tName\t \t\t\t\tDate\t\ tStatus\n";
    # Cycle through our list
    for $i (@Ideas ) {
    $file = $baseDir."/".$i."/$i.txt";

    open (LOGFILE, $file) or die "I couldn't open $file\n";

    $x = 0;
    while ( $x == 0 ) {
    $line = <LOGFILE> ;

    if ( $line =~ /^\n/ ) {
    $x=1;

    } else {
    @line = split(/:/, $line);
    $y = (length $line[1]) - 2 ;

    $tbs = ((length $line[1])-((length $line[1])%8)) ;
    # any attempt at division causes the program to bork on this line
    # comment the line, and it all runs fine
    # change to any other operand, and it runs fine
    # out of ideas -- wtf!!!
    $ts = int( $tbs / 8 ) ;

    switch ( $line[0] ) {
    case /^Status/ { print substr $line[1], 1, $y; print "\n"; }
    case /^Code/ { print substr $line[1], 1, $y; print "\t"; }
    case /^When/ { print substr $line[1], 1, $y; print "\t"; }
    case /^Name/ { print substr $line[1], 1, $y; print "\t"; }
    }
    }
    }
    close LOGFILE;

    # print "located $i\n";
    }
    [/CODE]

    if you need to create a test run, create a directory, with a file of the directory name.txt in it
    eg : ~/Desktop/IdeaBin/TestDir/TestDir.txt

    content template is:
    ;----------------
    Code: Test
    Name: Test Application
    Who: Some Body
    When: Jul 24, 2007
    Status: Open

    ;----------------
    Last edited by miller; Jul 24 '07, 09:50 PM. Reason: Code Tag and ReFormatting
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    instead of :

    Code:
    $tbs = ((length $line[1])-((length $line[1])%8)) ;

    try:

    Code:
    $tbs = length $line[1] - (length $line[1] %8 );
    and report back.

    Comment

    Working...