fill hash in loop

Posted on 2005-04-11
Last Modified: 2008-03-04
i am trying to use this code to fill a hash using a loop as part of a c comment remover that i am making.  i read all the characters in a file into an array,
test for the occurance of two characters( /*), if they occur next to eachother,
i store the location of the first in a variable.  then i test for two characters  (*/)
and if they occur next to eachother, i store the location of the last in a variable.

then, i try to put the two variable into a hash, one as the key, and another as the value.
then, i will eventually go through the hash, remove the keys and values and splice the original array using
these keys and values.  

i know there are easier ways to do this, but i am wondering why my hash contains extra values - i would
like to work with my algorithm, however awkward it may seem.


foreach $line(@lines) {

@string=split (//,$line);
push @c,@string;

close code1;
#print @c;

while ($i<@c){
if ((@c[$i]=~/\//) && (@c[$i + 1]='*')){
print "$p1";
print "@c[$i]\n";
if ((@c[$i]='*') && (@c[$i + 1]=~/\//)){
$p2=$i +1;
$hash{"$p1"} = "$p2";
print "$p2";
print "@c[$p2]\n";

print %hash;
#while ( my ($key, $value) = each(%hash) ) {
 #       print "$key => $value\n";
  #  }

Question by:worldfear
    LVL 84

    Accepted Solution

    is an assignment, which is always true.
    You probably wanted
    $c[$i] eq '*'

    Author Comment

    hey ozo, thanks very much.

    if you can, can you tell me why there is a space between every character in my array, and so
    i + 1 is always blank, but i + 2 finds the next intended character.

    great help.
    LVL 84

    Expert Comment

    There is not a space between every character in the arrays in the above code.
    Why do you believe i + 1 is always blank?

    Author Comment

    i use a junk file to test this code right now - it contains this text:

    dfalfkjasldfj; dsalfjasdjklf



    if i use i + 1, the code prints nothing - the if statements are never true.
    if i use i + 2, the if statements evaluate true, but the values of i stored in the hash are twice what i expect.

    LVL 84

    Expert Comment

    After changing = to eq in the above code, with the above text in code.txt, and running with warnings enabled, I get:
    Scalar value @c[$i] better written as $c[$i] at - line 18 (#1)
        (W syntax) You've used an array slice (indicated by @) to select a
        single element of an array.  Generally it's better to ask for a scalar
        value (indicated by $).  The difference is that $foo[&bar] always
        behaves like a scalar, both when assigning to it and when evaluating its
        argument, while @foo[&bar] behaves like a list when you assign to it,
        and provides a list context to its subscript, which can do weird things
        if you're expecting only one subscript.
        On the other hand, if you were actually hoping to treat the array
        element as a list, you need to look into how references work, because
        Perl will not magically convert between scalars and lists for you.  See
    Scalar value @c[$i + 1] better written as $c[$i + 1] at - line 18 (#1)
    Scalar value @c[$i] better written as $c[$i] at - line 21 (#1)
    Scalar value @c[$i] better written as $c[$i] at - line 23 (#1)
    Scalar value @c[$i + 1] better written as $c[$i + 1] at - line 23 (#1)
    Scalar value @c[$p2] better written as $c[$p2] at - line 27 (#1)

    Useless use of a variable in void context at - line 14 (#2)
        (W void) You did something without a side effect in a context that does
        nothing with the return value, such as a statement that doesn't return a
        value from a block, or the left side of a scalar comma operator.  Very
        often this points not to stupidity on your part, but a failure of Perl
        to parse your program the way you thought it would.  For example, you'd
        get this if you mixed up your C precedence with Python precedence and
            $one, $two = 1, 2;
        when you meant to say
            ($one, $two) = (1, 2);
        Another common error is to use ordinary parentheses to construct a list
        reference when you should be using square or curly brackets, for
        example, if you say
            $array = (1,2);
        when you should have said
            $array = [1,2];
        The square brackets explicitly turn a list value into a scalar value,
        while parentheses do not.  So when a parenthesized list is evaluated in
        a scalar context, the comma is treated like C's comma operator, which
        throws away the left argument, which is not what you want.  See
        perlref for more on this.
        This warning will not be issued for numerical constants equal to 0 or 1
        since they are often used in statements like
            1 while sub_with_side_effects() ;
        String constants that would normally evaluate to 0 or 1 are warned

    Author Comment

    thanks, ozo.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now