Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

concatenating variables

by Anonymous Monk
on Jul 31, 2012 at 17:23 UTC ( #984630=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello - I'm new to Perl. Trying to set up a loop to call a series of lines to submit to SQL I've already assigned $line1, $line2, $line3 etc with sql queries to run on the database. The problem is perl doesn't seem to like $linename.$counter
my $line1 = "use pricedb;"; my $linename = "line"; my $dbh = DBI->connect("DBI:mysql:host=localhost;database=mysql","root +","root"); # set up a loop to make submitting lines easier ; for(my $counter = 1; $counter <=10; $counter ++) { my $concatline = $linename.$counter; my $rowsaffected = $dbh ->do(${$concatline}); if ($rowsaffected && $rowsaffected == 0E0) { print "Operations ws successful but no rows were affecte +d \n"; } elsif($rowsaffected) { print "Operation was successful ($rowsaffected rows)\n"; } else { print "Operation failed: $DBI::errstr\n"; } } $dbh -> disconnect;
Please advise how I get the loop to pull $line1, $line2 etc? NB I'm very new to Perl so would appreciate v simple suggestions - my previous experience is SAS, Matlab and a little VBA..Thanks!!

Replies are listed 'Best First'.
Re: concatenating variables
by brx (Pilgrim) on Jul 31, 2012 at 17:37 UTC

    Very quick answer : don't 'forge' variable name. Use an array. If you realy need to 'forge' name, use an hash:

    my %names; $names{$linename.$counter} = "use pricedb;" ;

    With an array:

    #my $line1 = "use pricedb;"; my @lines = ( "use pricedb;" , "..." , "..." , ); for my $counter (1 .. 10) { my $rowsaffected = $dbh ->do($lines[$counter]); #...

    Better, without $counter:

    for my $line (@lines) { my $rowsaffected = $dbh ->do($line); #...

    English is not my mother tongue.
    Les tongues de ma mère sont "made in France".
      Many thanks all - works a treat - problem now solved, and am reading the 'Learning Perl' book... Best wishes..
Re: concatenating variables
by suaveant (Parson) on Jul 31, 2012 at 17:41 UTC
    There are ways to do this but it is generally considered bad practice to do so unless you have no other choice. Consider instead an array.
    $lines[0] = "use pricedb"; $lines[1] = "second statement"; for my $sql (@lines) { $dbh->do($sql); # check code here }
    That easily lends itself to pulling queries from a file or building queries programmatically. It also means you can have an arbitrary number of lines. I highly suggest getting
    Learning Perl
    The Camel Book
    If you are going to be doing any amount of Perl programming, both are very useful. Of course there are lots of online tutorials, too.

                    - Ant
                    - Some of my best work - (1 2 3)

Re: concatenating variables
by Khen1950fx (Canon) on Jul 31, 2012 at 21:18 UTC
    Primarily to test the efficacy of join, I tried it sans DBI, joining $linename and $counter. I used $i instead of $counter.
    #!/usr/bin/perl use autodie; use strictures 1; use Time::HiRes qw/sleep/; my $linename = 'line'; my $rowsaffected = 0; my $i = 1; my $max = 10; foreach $_ ( 1 .. $max ) { my $concatline = ( join( '', $linename, $i ) ); ++$concatline; sleep(0.01); if ( $rowsaffected == 0 ) { print "Operations were successful but no rows were affected"; } elsif ($rowsaffected) { print "Operation was successful ($rowsaffected rows)n"; } else { print "Operation failed: $!"; } } exit 0;

      Primarily to test the efficacy of join ..

      Stop trolling !

Re: concatenating variables
by DStaal (Chaplain) on Aug 01, 2012 at 20:16 UTC

    While you can probably do what you are trying to do, I strongly suspect you can get what you need with DBI placeholders/bind values, which will be a lot safer and clearer. (And, depending on the database, faster as well.)

    See the DBI man page for more information.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984630]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2020-07-03 23:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found