On 11/5/07, Patrik Hasibuan <[EMAIL PROTECTED]> wrote:

> I am trying to remove all the space (empty character) in a variable($k_tmp) 
> in > order to put them in an array, one cell only for one word.

You don't need to remove spaces to put words into an array. You sound
like you're asking for split:

   my @words = split /\s+/, $k_tmp;

....Although you may want a different pattern than I use, depending
upon your definition of "empty character".

> The value of the variable ($k_tmp) is a page of google which I did using
>  "UserAgent".

Do you mean that $k_tmp doesn't hold plain text, but instead has HTML?
That's much more complex to process, and you shouldn't try to use
simple regular expressions to work with it. Use an HTML-aware module.

> For identifying whether it's an 'empty displayed' value, I do so:
> foreach my $k_tmp(@kalimat){
>     $spasi=0;

Is this $spasi a global variable? You should be programming with 'use
strict' and 'use warnings' at the top of your program, and you should
be declaring local variables with my().

>     @k=split(" +",$k_tmp);
>     #####hapus sel berisi spasi######
>     foreach $k_tmp(@k){

Do you realize that you're re-using the $k_tmp variable from the outer
foreach loop? That's a confusing programming style, to say the least.

>         my $spasi=$k_tmp=~/ +/g;
>         my $garisbaru=$k_tmp=~/\n+/g;

I don't think you want to use m//g in a scalar context. It's useful
and powerful, but I don't think it's the tool for whatever you're
doing, at least not the way you're using it. Probably you want either
s/ +//g or m/ +/ without the /g option.

>         unless($spasi==1 or $garisbaru==1){
>             push(@k_isi,$k_tmp);
>         }

What's this @k_isi? Is it a global variable? Where was it initialized?
What is its purpose? (It might help if I knew what spasi and garisbaru
mean.... Spaces and newlines?)

>     }
>     #end of 'hapus sel berisi spasi'#
>     $spasi=0;
>     push(@kata,@k_isi);

The inner loop adds data to @k_isi, the outer one adds @k_isi to
@kata. If items in @k_isi on the first iteration aren't supposed to be
added to @kata again on every later time through the loop, where's the
step in which items are removed from @k_isi?

>     [EMAIL PROTECTED];
>     splice(@k,0,$z);

If I'm not mistaken, that splice() is the hard way to write this:

  unshift @k, $z;

>     [EMAIL PROTECTED];
>     splice(@k_isi,0,$z);
> }

...but what are you doing with $z? It seems that you're putting the
number of elements in the array onto the start of the array. That's
odd. That would imply that these arrays will be passed out of Perl; is
that what's going on? Is it the program you're passing the data to
that is giving you the error message you're seeing?

You said that that code is "For identifying whether it's an 'empty
displayed' value," but I can't see in what way it's doing that. Is it
part of a subroutine, and you haven't shown us the whole thing? What
are you leaving out? Are you not showing us some code that looks
something like this?

  if ($something == 42 or $something_else ne 'empty') {
    print "Everything is fine.\n";
  } else {
    print "It's 'empty displayed' now!\n";
  }

> But my program give output how I can not understand. I still have 'empty
> displayed' value of a variable in '@k_isi'.

"empty displayed" is not a Perl error message; is it coming from your
own code? You haven't shown us the code that's giving you that
message; you're showing us other code. Is this a different problem
you're having, or the same one?

> As far as I know, the sort of data/character/string those displayed empty
> space are only space (' ') and newline ('\n').

You probably mean "\n" for newline; there is no backslash-n newline in
single quotes.

> Please tell me if there is still other "things" which will be diplayed empty
> by "print $var".

Do you mean, you want to know what other characters might not use any
"ink" to print? Lots of them. Besides space, tab, and newline, there
are the non-breaking space, many other control characters, and many
many Unicode characters which might appear blank.

But the characters which most people use with the intention of being
whitespace are matched by the \s shortcut in patterns.

If you find a character that you call "empty" that \s doesn't match,
you can easily adapt your pattern to include that character. You may
be able to put it into the pattern by using cut-and-paste, but it may
be necessary or helpful to specify the character by a backslash
escape, once you find out which character it is.

  my @words = split /[\s\x08]+/, $data; # \s and backspace

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to