Converting a list join to a scaler/string

S

sdfgsd

Hello,

Did the sites, books and google and still can't figure this out:

my @quoted = map {"'$_', "} @fields;
my $insert = join ' ', @quoted;

I just need to "flatten" or make $insert an actual string

Thanks again for your help.
 
S

Sam Holden

Is that your real email address?

Do you have permission from the Strategic Decisions Group to
use that address.

Claiming such an address without permission from the owner is
illegal in some jurisdictions.

If you want an invalid address then use whatever.invalid or
example.(com|org|net).
Hello,

Did the sites, books and google and still can't figure this out:

my @quoted = map {"'$_', "} @fields;
my $insert = join ' ', @quoted;

I just need to "flatten" or make $insert an actual string

$insert is a string. Something like "'abc', 'def', 'ghi',".

It helps to mention what the problem is.

And to say what you expected and what your result actually was.
 
T

Tad McClellan

sdfgsd said:
my $insert = join ' ', @quoted;

I just need to "flatten" or make $insert an actual string


It already _is_ an actual string.

Problem solved!
 
S

sdfgsd

Sam Holden said:
Is that your real email address?

Do you have permission from the Strategic Decisions Group to
use that address.

Claiming such an address without permission from the owner is
illegal in some jurisdictions.

Ignorance is still a valid excuse right? Thanks for pointing it out, it's
changed. This news account was set up in haste.
$insert is a string. Something like "'abc', 'def', 'ghi',".

It helps to mention what the problem is.

The end goal is to build an SQL INSERT:

my @fields = qw(Name Address City State Zip Phone);
my @quoted = map {"'$_', "} @fields;
my $insert = join ' ', @quoted;

The problem is that I need $insert to be an actual string so that I can
concatenate it with a set of parenthesis and the rest of the INSERT. I then
need to push these INSERTS into a file for later use.

The expected output if I were to print $insert: ('name', 'address', ...)

Thanks for the response.
 
B

Ben Morrow

sdfgsd said:
The end goal is to build an SQL INSERT:

my @fields = qw(Name Address City State Zip Phone);
my @quoted = map {"'$_', "} @fields;
my $insert = join ' ', @quoted;

The problem is that I need $insert to be an actual string so that I can
concatenate it with a set of parenthesis and the rest of the INSERT. I then
need to push these INSERTS into a file for later use.

The expected output if I were to print $insert: ('name', 'address', ...)

$insert = join ', ', map { "'$_'" } @fields;
$insert = "($insert)";

Ben
 
S

Sam Holden

The end goal is to build an SQL INSERT:

my @fields = qw(Name Address City State Zip Phone);
my @quoted = map {"'$_', "} @fields;
my $insert = join ' ', @quoted;

The problem is that I need $insert to be an actual string so that I can
concatenate it with a set of parenthesis and the rest of the INSERT. I then
need to push these INSERTS into a file for later use.

The expected output if I were to print $insert: ('name', 'address', ...)

And that's what you'll get (well you want get the parenthesis and you'll
have a trailing comma - which can be fixed by moving the comma to the
join.

Tack:
print "$insert\n";
after those four lines of code and you'll get:

'Name', 'Address', 'City', 'State', 'Zip', 'Phone',

I can't see how that is not an "actual string".

As I said in my first reply:

What result do you want?

What result did you get?
 
M

Malcolm Dew-Jones

Sam Holden ([email protected]) wrote:

: Is that your real email address?

: Do you have permission from the Strategic Decisions Group to
: use that address.

$ dig sdg.com any
....
;; ANSWERS:
sdg.com. 172800 NS ns22.customer.level3.net.


-> level3

I would also recommend you don't use the sdg address - but for quite
different reasons.
 
S

sdfgsd

Sam Holden said:
....)

And that's what you'll get (well you want get the parenthesis and you'll
have a trailing comma - which can be fixed by moving the comma to the
join.

Tack:
print "$insert\n";
after those four lines of code and you'll get:

'Name', 'Address', 'City', 'State', 'Zip', 'Phone',

I can't see how that is not an "actual string".

As I said in my first reply:

What result do you want?

I want to strip the trailing comma and to be able to add to the string.

Given:
$mystring = "Hello there,";
$mystring =~ s/.$//s;
print $mystring; #Hello there <- No comma

my @fields = qw(Name Address City State Zip Phone);
my @quoted = map {"'$_', "} @fields;
my $insert = join ' ', @quoted; # This line does leave a trailing comma
$insert =~ s/.$//s;
print $insert; # 'Name', 'Address', 'City', 'State', 'Zip', 'Phone', <-
comma remains!

This leads me to believe that $insert is "not really a string".
What result did you get?

The comma could not be stripped from the string produced by the join

Thanks everyone for the responses.
 
A

Anno Siegel

sdfgsd said:
Sam Holden said:
I want to strip the trailing comma and to be able to add to the string.

Given:
$mystring = "Hello there,";
$mystring =~ s/.$//s;
print $mystring; #Hello there <- No comma

my @fields = qw(Name Address City State Zip Phone);
my @quoted = map {"'$_', "} @fields;
my $insert = join ' ', @quoted; # This line does leave a trailing comma
$insert =~ s/.$//s;
print $insert; # 'Name', 'Address', 'City', 'State', 'Zip', 'Phone', <-
comma remains!

This leads me to believe that $insert is "not really a string".


The comma could not be stripped from the string produced by the join

Of course not. Remember how you built the string? The parts were

my @quoted = map {"'$_', "} @fields;

These all end with a comma *and a blank*, and so does the joined string
you build from them. No mystery at all.

Anno
 
S

sdfgsd

Anno Siegel said:
Of course not. Remember how you built the string? The parts were

my @quoted = map {"'$_', "} @fields;

These all end with a comma *and a blank*, and so does the joined string
you build from them. No mystery at all.

Anno

OH Jeez!! Thanks.
 
J

J. Gleixner

sdfgsd said:
OH Jeez!! Thanks.

Now that you've stated you're trying to build SQL, there are better ways
to do what you want. Read a few of the many articles at:

http://www.perl.com/search/index.ncsp?sp-q=dbi&submit=Search

For instance:

http://www.perl.com/pub/a/2001/03/dbiokay.html

my $fields = join(', ', @fields);
my $values = join(', ', map { $dbh->quote($_) } @formdata{@fields});
my $sql = "INSERT into $table ($fields) values ($values)";

Can also use placeholders, and not have to worry about calling quote
directly, to make things even nicer. That's also covered in that article.
 
S

sdfgsd

J. Gleixner said:
sdfgsd wrote:

Now that you've stated you're trying to build SQL, there are better ways
to do what you want. Read a few of the many articles at:

http://www.perl.com/search/index.ncsp?sp-q=dbi&submit=Search

For instance:

http://www.perl.com/pub/a/2001/03/dbiokay.html

my $fields = join(', ', @fields);
my $values = join(', ', map { $dbh->quote($_) } @formdata{@fields});
my $sql = "INSERT into $table ($fields) values ($values)";

Wow. This is a lot easier. Thanks
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top