On Monday 05 November 2007 09:47, Patrik Hasibuan wrote:
> Dear my friends...

Hello,

> I am exhausted by a programming case which may be easy for you all. 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. The value of the variable ($k_tmp) is a page of google
> which I did using "UserAgent".
>
> For identifying whether it's an 'empty displayed' value, I do so:
> foreach my $k_tmp(@kalimat){
>     $spasi=0;
>     @k=split(" +",$k_tmp);

We usually delimit regular expressions with // instead of "".


>     #####hapus sel berisi spasi######
>     foreach $k_tmp(@k){

You are using the same variable name ($k_tmp) as the outer foreach 
loop.  You should use a different name and use my() to make it a 
lexical variable.


>         my $spasi=$k_tmp=~/ +/g;

Because of the split() above there will be no space characters in 
$k_tmp and $spasi will *always* be false.

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

The expression $k_tmp=~//g in scalar context will return true or false 
(1 or '').  The expression /\n/ would be more efficient than /\n+/g 
(because 1 "\n" character is just as true as 1,000 "\n" characters.)

Or you could use index() instead.


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

So @k_isi will only contain strings that don't have a "\n" in them:

      my @k_isi = grep !/\n/, split / +/, $k_tmp;


>     #end of 'hapus sel berisi spasi'#
>     $spasi=0;
>     push(@kata,@k_isi);
>     [EMAIL PROTECTED];
>     splice(@k,0,$z);

Or just:

      @k = ();


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

Or just:

      @k_isi = ();

Or you could push() *and* clear out the array at the same time:

     push @kata, splice @k_isi;


> }
>
> But my program give output how I can not understand. I still have
> 'empty displayed' value of a variable in '@k_isi'.
>
> As far as I know, the sort of data/character/string those displayed
> empty space are only space (' ') and newline ('\n').
>
> Please tell me if there is still other "things" which will be
> diplayed empty by "print $var".

It sounds like you are getting a warning about an uninitialized value, 
something like:

$ perl -Mwarnings -le' my $x; print "$x" '
Use of uninitialized value in string at -e line 1.

perldoc -f undef
perldoc -f defined


When you use split:

@k=split(" +",$k_tmp);

and $k_tmp contains space characters at the beginning like "  some 
text" then the array @k will have a zero length string at the beginning 
like ( '', 'some', 'text' ) and that could be the 'empty displayed' 
value you are seeing.




John
-- 
use Perl;
program
fulfillment

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


Reply via email to