join array elements into a string

P

Perl Lover

I want to join elements of an array into a string.
I can use join function, but the problem is that
one or more elements of the array can contain
undef values.
join function spews out error when one of the
array elements is undef.

Is there a better way.

Basically the array elements are data from a RDBMS table
coming via fetchow_array call of DBI. fetrow_array returns
undef for those columns which have null values from the RDBMS.

I am comparing two arrays to determine whether they are same or different.
Speed is utmost importance as the volumne of data
is very large.

thanks.
 
J

John W. Krahn

Perl said:
I want to join elements of an array into a string.
I can use join function, but the problem is that
one or more elements of the array can contain
undef values.
join function spews out error when one of the
array elements is undef.

Do you mean like this:

$ perl -le'
use warnings;
use strict;
my @array = ( "one", "two", undef, "four", undef, "six" );
my $string = join "", @array;
print $string;

'
Use of uninitialized value in join or string at -e line 5.
Use of uninitialized value in join or string at -e line 5.
onetwofoursix

Is there a better way.

$ perl -le'
use warnings;
use strict;
my @array = ( "one", "two", undef, "four", undef, "six" );
my $string = join "", grep defined, @array;
print $string;

'
onetwofoursix



John
 
J

Jürgen Exner

Perl said:
I want to join elements of an array into a string.
I can use join function, but the problem is that
one or more elements of the array can contain
undef values.
join function spews out error when one of the
array elements is undef.

Why don't you simple grep() the defined() values?
Is there a better way.

Don't know if better, but as an alternative you could simple stringify the
array:
$foo = "@array";
Basically the array elements are data from a RDBMS table
coming via fetchow_array call of DBI. fetrow_array returns
undef for those columns which have null values from the RDBMS.

I am comparing two arrays to determine whether they are same or
different. Speed is utmost importance as the volumne of data
is very large.

You are aware that concatenating all array elements and then comparing for
equality will result in false positives, are you?
Example:
['x', 'yz'] and ['xy', 'z'] are certainly different, while 'xyz' and
'xyz' are the same.

jue
 
S

Sisyphus

Perl Lover said:
I want to join elements of an array into a string.
I can use join function, but the problem is that
one or more elements of the array can contain
undef values.
join function spews out error when one of the
array elements is undef.

That's not correct. What you see is a warning message telling you that there
were uninitialized values. Nothing more, nothing less - but there is *no*
error.

In addition to the other (probably preferable) solutions already proposed,
you could simply disable warnings for uninitialized values:

{
no warnings "uninitialized";
join ...... ;
}

Cheers,
Rob
 
F

foo bar baz qux

Purl said:
Perl Lover wrote:

(snipped)


Other words, are two arrays equal? You have not stated "similar" nor "alike"
rather "same or different."

if ("@Array1" eq "@Array2")
{ print "Match"; }
else
{ print "No Match"; }


#!perl
use warnings;
use strict;
my @Array1 = ( "a", "a", "a", "a", "a", "a" ); # 6 elements
my @Array2 = ( "a a", "a", "a", "a", "a" ); # 5 elements

if ("@Array1" eq "@Array2")
{ print "Match"; }
else
{ print "No Match"; }

C:>perl arrayequal.pl
Match

ROFL - PG considers a six element array to be "equal" to a five element
array.
 
D

Dr.Ruud

Jürgen Exner schreef:
as an alternative you could simple
stringify the array:
$foo = "@array";

That will equalize qw/a b c/ and ("a b", "c").

If all elements in the array can only contain printable characters, then
something like this is an alternative:

$foo = do { no warnings 'uninitialized'; local $" = $;; "@array" }

See perlvar: LIST_SEPARATOR, SUBSCRIPT_SEPARATOR.
 
J

Jürgen Exner

Dr.Ruud said:
Jürgen Exner schreef:


That will equalize qw/a b c/ and ("a b", "c").

1: Which is the same behaviour as the OP would get with his approach using
join()
2: Which I actually pointed out in the part you snipped

If this is a behaviour he wants or not is his call

jue
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top