join array elements into a string

Discussion in 'Perl Misc' started by Perl Lover, Jan 6, 2007.

  1. Perl Lover

    Perl Lover Guest

    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.
     
    Perl Lover, Jan 6, 2007
    #1
    1. Advertising

  2. Perl Lover wrote:
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Jan 6, 2007
    #2
    1. Advertising

  3. Perl Lover

    DJ Stunks Guest

    Perl Lover wrote:
    > I am comparing two arrays to determine whether they are same or different.


    perldoc -q equal

    -jp
     
    DJ Stunks, Jan 6, 2007
    #3
  4. Perl Lover wrote:
    > 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
     
    Jürgen Exner, Jan 6, 2007
    #4
  5. Perl Lover

    Sisyphus Guest

    "Perl Lover" <> wrote in message
    news:459f2218$0$97260$...
    > 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
     
    Sisyphus, Jan 6, 2007
    #5
  6. Purl Gurl wrote:
    > Perl Lover wrote:
    >
    > (snipped)
    >
    > > I am comparing two arrays to determine whether they are same or different.

    >
    > 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.
     
    foo bar baz qux, Jan 6, 2007
    #6
  7. Perl Lover

    Dr.Ruud Guest

    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.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 6, 2007
    #7
  8. Dr.Ruud wrote:
    > 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").


    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
     
    Jürgen Exner, Jan 6, 2007
    #8
  9. Perl Lover

    Perl Lover Guest

    Thank you all for various suggestions.

    regards.
     
    Perl Lover, Jan 6, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    898
    Alan Silver
    Jun 5, 2006
  2. googleboy
    Replies:
    1
    Views:
    937
    Benji York
    Oct 1, 2005
  3. Jorge

    horizontal join of array elements

    Jorge, Dec 8, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    65
    Jorge
    Dec 8, 2004
  4. Jorge

    horizontal join of array elements

    Jorge, Dec 8, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    124
    Jorge
    Dec 8, 2004
  5. Tradeorganizer

    how to join array into array

    Tradeorganizer, May 6, 2007, in forum: Perl Misc
    Replies:
    12
    Views:
    244
    Michele Dondi
    May 11, 2007
Loading...

Share This Page