For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > Concatenating Strings









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 Concatenating Strings
Bobby

2008-03-24, 7:05 pm

Hi all,

I'm trying to write a simple do until loop to print out the value of $strA0 through $striA3. What i'm doing is replacing the value of 0 through 3 in the $strA by joining two strings (my $strB = "strA" . $count;). Right now my script is printing $strB as
below. How do i get perl to print the value of $strA0 through $strA3 inside of my do until loop? i.e.:

Desired Outcome:
VarB: A0
VarB: A1,b,c
VarB: A 2
VarB: A3,d,e

Current Outcome:
VarB:strA0
VarB:strA1
VarB:strA2
VarB:strA3


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

my $strA0="A0";
my $strA1="A1,b,c";
my $strA2="A2";
my $strA3="A3,d,e";

my $count = 0;
until ($count == 4){
my $strB = "strA" . $count;
print "VarB:$strB\n";
$count++;
}#end until loop

Thanks,

Mike








---------------------------------
Never miss a thing. Make Yahoo your homepage.
Wolf

2008-03-24, 7:05 pm


---- Bobby <cybercruiserz@yahoo.com> wrote:
> Wolf,
>
> I still don't understand, so set my $strB = "$strA($count)"; ? That didn't worked.
>
> Wolf <lonewolf@nc.rr.com> wrote:
> ---- Bobby wrote:
as below. How do i get perl to print the value of $strA0 through $strA3 inside of my do until loop? i.e.:[color=darkred]
>
> You can either turn them into an array and walk the array with your count
>
> You are setting $strB as the string strA instead of $strA($count) which is where you are getting hosed up.
>
> My perl server is down or I'd play with the code a bit to get it the exact answer, but that should point you in the right direction.
>
> HTH,
> Wolf
>


No Bobby, more along the lines of:
my $B = "strA" .$count;
my $strB = $B;
print "VarB:$strB\n";

But you are going to have to keep playing with it if it doesn't work right.

Wolf
Yitzle

2008-03-24, 7:05 pm

IIRC, Perl does not let you use a string to build a variable name like PHP does.
If you do this:
$myVar = 123;
$varName = "myVar";
print "$varName";
You get "myVar" and not "123" which seems to be what you want.
However, I think you might be able to use hashes and get what you want.
$hash{"myVar"} = "123";
$varName = "myVar";
print "$hash{$varName}";
That'll give you "123".
What exactly are you trying to accomplish?
Bobby

2008-03-24, 7:05 pm

Thanks for both of your suggestions. I've tried them both but still getting same result. Any other suggestions?

Thanks.

Wolf <lonewolf@nc.rr.com> wrote:
---- Bobby wrote:
> Wolf,
>
> I still don't understand, so set my $strB = "$strA($count)"; ? That didn't worked.
>
> Wolf wrote:
> ---- Bobby wrote:
as below. How do i get perl to print the value of $strA0 through $strA3 inside of my do until loop? i.e.:[color=darkred]
>
> You can either turn them into an array and walk the array with your count
>
> You are setting $strB as the string strA instead of $strA($count) which is where you are getting hosed up.
>
> My perl server is down or I'd play with the code a bit to get it the exact answer, but that should point you in the right direction.
>
> HTH,
> Wolf
>


No Bobby, more along the lines of:
my $B = "strA" .$count;
my $strB = $B;
print "VarB:$strB\n";

But you are going to have to keep playing with it if it doesn't work right.

Wolf

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/





---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
Wolf

2008-03-24, 7:05 pm

Grab a perl book or search the web for concatenating variables together...

Unfortunately the current place I am in doesn't have perl on the server for me to try and play around more, but the gist is the same. Your problem arise from setting it up as string during the concatination process.

Set the responses in an array and just walk the array for a quick and dirty solution.


---- Bobby <cybercruiserz@yahoo.com> wrote:
> Thanks for both of your suggestions. I've tried them both but still getting same result. Any other suggestions?
>
> Thanks.
>
> Wolf <lonewolf@nc.rr.com> wrote:
> ---- Bobby wrote:
rB as below. How do i get perl to print the value of $strA0 through $strA3 inside of my do until loop? i.e.:[color=darkred]
>
> No Bobby, more along the lines of:
> my $B = "strA" .$count;
> my $strB = $B;
> print "VarB:$strB\n";
>
> But you are going to have to keep playing with it if it doesn't work right.
>
> Wolf
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
>
>
> ---------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.


Gunnar Hjalmarsson

2008-03-24, 7:05 pm

Bobby wrote:
> I'm trying to write a simple do until loop to print out the value of
> $strA0 through $striA3. What i'm doing is replacing the value of 0
> through 3 in the $strA by joining two strings (my $strB = "strA" .
> $count;). Right now my script is printing $strB as below. How do i
> get perl to print the value of $strA0 through $strA3 inside of my do
> until loop? i.e.:
>
> Desired Outcome:
> VarB: A0
> VarB: A1,b,c
> VarB: A 2
> VarB: A3,d,e
>
> Current Outcome:
> VarB:strA0
> VarB:strA1
> VarB:strA2
> VarB:strA3
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $strA0="A0";
> my $strA1="A1,b,c";
> my $strA2="A2";
> my $strA3="A3,d,e";
>
> my $count = 0;
> until ($count == 4){
> my $strB = "strA" . $count;
> print "VarB:$strB\n";
> $count++;
> }#end until loop


You are trying to use symbolic references. To make it work, you'd need to
- declare the $strAx variables with our() instead of my(),
- disable strictures ("no strict 'refs';"),
- dereference the strings: print "VarB:$$strB\n".

Please study the FAQ entry

perldoc -q "variable name"

to find out why that's not a recommended approach. You probably ought to
make use of a hash instead.

my %strA = (
0 => 'A0',
1 => 'A1,b,c',
2 => 'A2',
3 => 'A3,d,e',
);

print "VarB:$strA{$_}\n" foreach 0..3;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Kens

2008-03-24, 7:05 pm

On Mar 24, 12:09 pm, cybercruis...@yahoo.com (Bobby) wrote:
> Hi all,
>
> I'm trying to write a simple do until loop to print out the value of $strA0 through $striA3. What i'm doing is replacing the value of 0 through 3 in the $strA by joining two strings (my $strB = "strA" . $count;). Right now my script is printing $strB a

s below. How do i get perl to print the value of $strA0 through $strA3 inside of my do until loop? i.e.:
>
> Desired Outcome:
> VarB: A0
> VarB: A1,b,c
> VarB: A 2
> VarB: A3,d,e
>
> Current Outcome:
> VarB:strA0
> VarB:strA1
> VarB:strA2
> VarB:strA3
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $strA0="A0";
> my $strA1="A1,b,c";
> my $strA2="A2";
> my $strA3="A3,d,e";
>
> my $count = 0;
> until ($count == 4){
> my $strB = "strA" . $count;
> print "VarB:$strB\n";
> $count++;
>
> }#end until loop
>
> Thanks,
>
> Mike
>
> ---------------------------------
> Never miss a thing. Make Yahoo your homepage.


Mike,
I'm not sure why you are attempting to solve your problem without an
array as suggested by Wolf:

use strict;
use warnings;

my @strA = ('A0', 'A1,b,c', 'A2', 'A3,d,e');

foreach my $strB ( @strA )
{
print "$strB\n";
}


# Or if you must use a counter

my $count = 0;

while ( defined($strA[$count]) )
{
print "$strA[$count++]\n";
}

HTH, Ken

Gunnar Hjalmarsson

2008-03-24, 7:05 pm

yitzle wrote:
> IIRC, Perl does not let you use a string to build a variable name like PHP does.


Then you do not remember it correctly.

> If you do this:
> $myVar = 123;
> $varName = "myVar";
> print "$varName";
> You get "myVar" and not "123"


Sure, but if you replace the last line with

print $$varName;

it outputs '123' under certain conditions. Please see my other message
in this thread about why that approach is not a good idea.

> However, I think you might be able to use hashes and get what you want.


Indeed.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Rob Dixon

2008-03-24, 10:08 pm

Bobby wrote:
> Hi all,
>
> I'm trying to write a simple do until loop to print out the value of
> $strA0 through $striA3. What i'm doing is replacing the value of 0
> through 3 in the $strA by joining two strings (my $strB = "strA".
> $count;). Right now my script is printing $strB as below. How do i get
> perl to print the value of $strA0 through $strA3 inside of my do until
> loop? i.e.:
>
> Desired Outcome:
> VarB: A0
> VarB: A1,b,c
> VarB: A 2
> VarB: A3,d,e
>
> Current Outcome:
> VarB:strA0
> VarB:strA1
> VarB:strA2
> VarB:strA3
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $strA0="A0";
> my $strA1="A1,b,c";
> my $strA2="A2";
> my $strA3="A3,d,e";
>
> my $count = 0;
> until ($count == 4){
> my $strB = "strA" . $count;
> print "VarB:$strB\n";
> $count++;
> }#end until loop


Here's how, but you really /really/ don't want to do it.

HTH,

Rob


use strict;
use warnings;

our ($strA0, $strA1, $strA2, $strA3);

$strA0 = 'A0';
$strA1 = 'A1,b,c';
$strA2 = 'A2';
$strA3 = 'A3,d,e';

for my $count (0 .. 3) {
my $strB = 'strA' . $count;
{
no strict 'refs';
print "VarB:$$strB\n";
}
}
Randal L. Schwartz

2008-03-24, 10:08 pm

>>>>> "Bobby" == Bobby <cybercruiserz@yahoo.com> writes:

Bobby> I'm trying to write a simple do until loop to print out the value of
Bobby> $strA0 through $striA3.

If your variable names are named sequentially, you've almost always done
something wrong. Please rethink your problem, keeping data structures in
mind.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Rob Dixon

2008-03-24, 10:08 pm

kens wrote:
>
> # Or if you must use a counter
>
> my $count = 0;
>
> while ( defined($strA[$count]) )
> {
> print "$strA[$count++]\n";
> }


for my $count (0 .. $#strA) {
print "[$count] = $strA[$count]\n";
}

**OR**

my $count = 0;

foreach (@strA) {
print "[$count] = $strA[$count]\n";
$count++;
}



Rob
Bobby

2008-03-25, 7:09 pm

Thanks for all your input, using a hash does make things a lot easier for me.

Rob Dixon <rob.dixon@gmx.com> wrote: kens wrote:
>
> # Or if you must use a counter
>
> my $count = 0;
>
> while ( defined($strA[$count]) )
> {
> print "$strA[$count++]\n";
> }


for my $count (0 .. $#strA) {
print "[$count] = $strA[$count]\n";
}

**OR**

my $count = 0;

foreach (@strA) {
print "[$count] = $strA[$count]\n";
$count++;
}



Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/





---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
Sponsored Links







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

Copyright 2008 codecomments.com