Iterate through words in a string

G

Gonzalo Castro

I'm have a string I am constructing with space delimited words. I would like
to iterate through each word later. I thought making an array from the
contents of the string would be the convenient way to approach this but I'm
not having any luck. Any ideas?

I construct a string in code similar to this:
$accumulator = ""
while (some condition)
{
$anotherString = getMeAnotherString();
$accumulator = $accumulator . " $anotherString"
}


Then I try to make an array but this is clearly not right. What's the right
way to do this?
@array = qw($accumulator);

When I iterate
for $element (@array)
{
print "element = $element";
}

I get this:
element = $accumulator

You might ask, "Why not simply add/push $anotherString onto an array first
instead of concatenating into a string?" And I would answer, "Well, because
I want the list of words to be the value part of a key/value hash and values
have to be scalar. Am I right?"

Thanks,
Gonzalo
 
G

Gunnar Hjalmarsson

Gonzalo said:
I'm have a string I am constructing with space delimited words. I would like
to iterate through each word later. I thought making an array from the
contents of the string would be the convenient way to approach this but I'm
not having any luck. Any ideas?

I construct a string in code similar to this:
$accumulator = ""
while (some condition)
{
$anotherString = getMeAnotherString();
$accumulator = $accumulator . " $anotherString"
}


Then I try to make an array but this is clearly not right. What's the right
way to do this?
@array = qw($accumulator);

my @array = split ' ', $accumulator;

perldoc -f split
You might ask, "Why not simply add/push $anotherString onto an array first
instead of concatenating into a string?" And I would answer, "Well, because
I want the list of words to be the value part of a key/value hash and values
have to be scalar. Am I right?"

No.

my @array = qw(green red blue);
my %hash;
@hash{ qw/w1 w2 w3/ } = @array;
 
J

John Bokma

When I iterate
for $element (@array)
{
print "element = $element";
}

I get this:
element = $accumulator

Yup,

perldoc -f qw (which sends you somewhere else)
perldoc -f split (to answer your q)
You might ask, "Why not simply add/push $anotherString onto an array

Nah, I ask: why you're using camelCase in Perl? Try not to use a style
that's common in one language into another (in Perl _ seems to be
preferred).
 
P

Paul Lalli

Gonzalo said:
You might ask, "Why not simply add/push $anotherString onto an array first
instead of concatenating into a string?" And I would answer, "Well, because
I want the list of words to be the value part of a key/value hash and values
have to be scalar. Am I right?"

Your words are correct, but your meaning is not. Hash values must be
scalars, yes. They do not, however, have to be strings. References
(such as array references) are also scalars.

Please read up on references and multi-dimensional structures in:
perldoc perlreftut
perldoc perldsc

#!/usr/bin/perl
use strict;
use warnings;
my @words = ("foo", "bar", "baz");
my %hash;
$hash{alpha} = \@words;
print "First word: $hash{alpha}[0];
print "All words: @{$hash{alpha}}\n";
__END__

Paul Lalli
 
D

DJ Stunks

Gonzalo said:
I'm have a string I am constructing with space delimited words. I would like
to iterate through each word later. I thought making an array from the
contents of the string would be the convenient way to approach this but I'm
not having any luck. Any ideas?

I construct a string in code similar to this:
$accumulator = ""
while (some condition)
{
$anotherString = getMeAnotherString();
$accumulator = $accumulator . " $anotherString"
}


Then I try to make an array but this is clearly not right. What's the right
way to do this?
@array = qw($accumulator);

When I iterate
for $element (@array)
{
print "element = $element";
}

I get this:
element = $accumulator

You might ask, "Why not simply add/push $anotherString onto an array first
instead of concatenating into a string?" And I would answer, "Well, because
I want the list of words to be the value part of a key/value hash and values
have to be scalar. Am I right?"

I think what everyone is saying is that yes, there is definitely a
better way to do what you're trying to do, but since we don't know what
your ultimate goal is we can't make specific suggestions.

Why don't you try showing us your sample input and desired output and
your current approach and we can help you see that there's a better way
to accomplish your goal.

-jp
 
G

Gonzalo Castro

perldoc perlreftut described exactly what I was trying to accomplish; use
arrays as hash values. After learning I could use references to refer to the
arrays I didn't need to concatenate and split a string as a workaround.

Thanks to you and others for getting me out of the fog.

Gonzalo



Gonzalo said:
You might ask, "Why not simply add/push $anotherString onto an array first
instead of concatenating into a string?" And I would answer, "Well, because
I want the list of words to be the value part of a key/value hash and values
have to be scalar. Am I right?"

Your words are correct, but your meaning is not. Hash values must be
scalars, yes. They do not, however, have to be strings. References
(such as array references) are also scalars.

Please read up on references and multi-dimensional structures in:
perldoc perlreftut
perldoc perldsc

#!/usr/bin/perl
use strict;
use warnings;
my @words = ("foo", "bar", "baz");
my %hash;
$hash{alpha} = \@words;
print "First word: $hash{alpha}[0];
print "All words: @{$hash{alpha}}\n";
__END__

Paul Lalli
 
J

John W. Krahn

Mirco said:
Thus spoke Gonzalo Castro (on 2007-01-04 19:57):


Better done like this (using your style and making a working example):

use strict;
use warnings;

sub getMeAnotherString { time }

my $accumulator;
my $done = 5;

while (not !$done--) {

Just as with the English language, you shouldn't use a double negative. :)

while ($done--) {



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top