Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Aggregating text and variables into a new variable

by finhagen (Sexton)
on Nov 29, 2008 at 18:06 UTC ( #726811=perlquestion: print w/ replies, xml ) Need Help??
finhagen has asked for the wisdom of the Perl Monks concerning the following question:

I would like to aggregate a string of text and variables into a new variable and can't seem to arrive at the correct syntax:
my $var1="fie; my $var2="fum"; $newvariable="fee $var1 fo $var2"; print "$newvariable\n";
I have tried putting my string into a array first:
@newarray=(fee $var1 fo $var2); print "$newarray[0],$newarray[1],$newarray[2],$newarray[3]\n"; $gser2="$newarray[0],$newarray[1],$newarray[2],$newarray[3]";
I am sure its just a matter of using the correct apostrophes but so far my own efforts have not been rewarded with success.


For the sake of clarity, here is the real syntax I am attempting to use:
my @gser=('TME GSER', $dealn, 'PAS 927922','SO',$so); my $gser2= join ';', $gser[0],$gser[1],$gser[2],$gser[3],$gser[4]; worksheet->write_string(2, 4, $gsers); #input string into excel cell
As usual the wisdom of the perl monks is deeply appreciated,


Hagen Finley
Boulder, CO

Comment on Aggregating text and variables into a new variable
Select or Download Code
Re: Aggregating text and variables into a new variable
by ccn (Vicar) on Nov 29, 2008 at 18:13 UTC
    There are several ways:
    use strict; # # first # my $var1="fie"; # note closing " my $var2="fum"; my $newvariable="fee $var1 fo $var2"; print "$newvariable\n"; # # second # my $newvariable2= join ' ', 'fee', $var1, 'fo', $var2; print "$newvariable2\n"; # # third # my $newvariable3= <<EOT; fee $var1 fo $var2 EOT print "$newvariable3\n"; # # fourth # my $newvariable4= 'fee ' . $var1 . ' fo ' . $var2; print "$newvariable4\n"; # # fifth # my $newvariable5= 'fee fo '; substr($newvariable5, 8, 0) = $var2; substr($newvariable5, 4, 0) = $var1; print "$newvariable5\n"; # # sixth # my $newvariable6 = pack 'A4A3A4A3', 'fee ', $var1, ' fo ', $var2; print "$newvariable6\n";

    see perldoc perlop about interpolation in Quote-and-Quote-like-Operators and concatenation operator .

    also please don't forget to use strict; in begin of your scripts

    and, imho, the good style is to use single quotes not double quotes for string literals which are not supposed to be interpolated.

      Thank you for your thorough reply. My problem is solved.
      Hagen
        It would be nice if you shared with us how you solved it. Someone else might learn something from it reading this thread.
Re: Aggregating text and variables into a new variable
by toolic (Chancellor) on Nov 29, 2008 at 20:00 UTC
    This:
    my $gser2= join ';', $gser[0],$gser[1],$gser[2],$gser[3],$gser[4];

    Can be shortened to this:

    my $gser2 = join ';', @gser[0..4];

    or even this:

    my $gser2 = join ';', @gser;

    Update: Another way is to use the special variable $" to stringify an array:

    use strict; use warnings; { local $" = q{;}; # locally redefine list separator my @gser = 1..5; my $gser2 = "@gser"; # the double quotes force interoplation print "gser2 = $gser2\n"; } __END__ gser2 = 1;2;3;4;5
Re: Aggregating text and variables into a new variable
by shmem (Canon) on Nov 29, 2008 at 20:25 UTC

    apart from the answers given so far,

    my @gser=('TME GSER', $dealn, 'PAS 927922','SO',$so); my $gser2= join ';', $gser[0],$gser[1],$gser[2],$gser[3],$gser[4]; worksheet->write_string(2, 4, $gsers); #input string into excel cell

    - do you really mean $gsers in the last line? shouldn't that be $gser2 ?

    Be consistent, above all ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://726811]
Approved by ccn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-27 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls