fill hash in loop

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";
  #  }

Who is Participating?
ozoConnect With a Mentor Commented:
is an assignment, which is always true.
You probably wanted
$c[$i] eq '*'
worldfearAuthor Commented:
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.
There is not a space between every character in the arrays in the above code.
Why do you believe i + 1 is always blank?
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

worldfearAuthor Commented:
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.

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
worldfearAuthor Commented:
thanks, ozo.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.