• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

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.
thanks



open(code1,"code.txt");
@lines=<code1>;


foreach $line(@lines) {

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


close code1;
#print @c;
%hash;
$i=0;

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

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





0
worldfear
Asked:
worldfear
  • 3
  • 3
1 Solution
 
ozoCommented:
@c[$i]='*'
is an assignment, which is always true.
You probably wanted
$c[$i] eq '*'
0
 
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.
0
 
ozoCommented:
There is not a space between every character in the arrays in the above code.
Why do you believe i + 1 is always blank?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
worldfearAuthor Commented:
i use a junk file to test this code right now - it contains this text:
/*dfgsfgdsfg*/

dfalfkjasldfj; dsalfjasdjklf

/*dsfaldshfakldfj;asjfl;

fsdlafajsdfkljasfklasdklfj;lakdfjaksdf*/

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.

0
 
ozoCommented:
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
    perlref.
   
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
    said
   
        $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
    about.
   
0/
13/
46/
111/
0
 
worldfearAuthor Commented:
thanks, ozo.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now