For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > somthing wrong with my codes???









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author somthing wrong with my codes???
Chen Li

2006-01-21, 3:55 am

Hi all,

I write a small script for practice purpose.
I have a string line in this format:
$string="password=xyz verbose=9 score=0";
I want to parse out password, verbose, and score into
one array and xyz, 9 and 0 into another array. But I
don't get what I expect. I just wonder what is wrong?

Thanks,

Li

This is my sctipt:

#!/usr/bin/perl
use warnings;
use strict;

my $string="password=xyz verbose=9 score=0";
my @keys=();
my @values=();
while ($string=~/(\w+)=(\w+)/g){

@keys=push(@keys, $1);
print $1, "\n";
@values=push(@values,$2);
print $2, "\n";
}
print "\nThese are the keys:","@keys";

print "\nThese are the values:","@values","\n";
exit;

And these are the results after I run the script:

password
xyz
verbose
9
score
0

These are the keys:2
These are the values:2





________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
usenet@DavidFilmer.com

2006-01-21, 3:55 am

Chen Li wrote:
> don't get what I expect. I just wonder what is wrong?


Well, for one thing:

@keys=push (@keys, $1);

isn't what you really want to do. You would have better luck with:

push (@keys, $1);

The push operation affects the array without an assignmant operator
(=). If a push() is successful, it returns the number of elements in
the array (ie, 2). So what your statement did was define that @keys
was equal to the value returned by the push operation (a scalar 2).
Each time through the loop, you re-defined the array (wiping out what
was in it before) because you kept using an assignment.

--
http://DavidFilmer.com

usenet@DavidFilmer.com

2006-01-21, 3:55 am

usenet@DavidFilmer.com wrote:[color=darkred]
> Chen Li wrote:

Also, any time you recognize a concept such as "keys" and "values"
within your data, the normal data structure in which to store those
"associated things" would be a hash (an associative array). You're
using two plain arrays (stacks), which is very odd for this sort of
thing.

Brano Gerzo

2006-01-21, 7:55 am

chen li [cl], on Friday, January 20, 2006 at 21:33 (-0800 (PST)) wrote
these comments:

cl> one array and xyz, 9 and 0 into another array. But I
cl> don't get what I expect. I just wonder what is wrong?

nothing is wrong, you wrote good script. But if you want print values,
use at beginning:

$|++; #dont buffer output

also learn what context is., If you want print array, try:

print "@array";
print @array;
$, = "-";
print @array;

for finding what you really have in variables use:

use Data::Dumper;

print Dumper \@array;

--

...m8s, cu l8r, Brano.

[Any woman is dateable if you have the right tickets.]


Shawn Corey

2006-01-21, 6:56 pm

chen li wrote:
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $string="password=xyz verbose=9 score=0";
> my @keys=();
> my @values=();
> while ($string=~/(\w+)=(\w+)/g){
>
> @keys=push(@keys, $1);


push @keys, $1;

> print $1, "\n";
> @values=push(@values,$2);


push @values, $2;

> print $2, "\n";
> }
> print "\nThese are the keys:","@keys";
>
> print "\nThese are the values:","@values","\n";
> exit;


__END__

See `perldoc -f push` for details, especially the part about the
returned value.


--

Just my 0.00000002 million dollars worth,
--- Shawn

"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com