Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Joining if an array

by Rodster001 (Pilgrim)
on May 01, 2009 at 15:29 UTC ( [id://761312]=perlquestion: print w/replies, xml ) Need Help??

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

This is more of a style question I suppose. But, is there a more concise/prettier way of writing this? How would you do it?
my $in; $in = join(",",@{$scalar}) if $scalar =~ /array/i; $in = $scalar if !$in;
Thanks!

Replies are listed 'Best First'.
Re: Joining if an array
by kennethk (Abbot) on May 01, 2009 at 15:35 UTC
    Here's one line that does exactly the same thing as your code using the ternary Conditional Operator.

    my $in = ($scalar =~ /array/i) ? join(",",@$scalar) : $scalar;

    Update: It occurs to me that by using a regex there, you are opening yourself up to a bug if $scalar contains the substring "array", so you should probably use the ref operator. And as Mutant points out below, if you are going for prettiness and legibility, an if-else is far clearer in intent than ? will be.

      Good point. I've used a regexp out of habit for a long time... from here on out I'll use ref().
Re: Joining if an array
by derby (Abbot) on May 01, 2009 at 15:36 UTC

    I'm not sure what you're trying to accomplish with the regex, but you want to use the ternary operator:

    $in = ref( $scalar ) eq "ARRAY" ? join( ",", @$scalar ) : $scalar;

    -derby

    Update: Hot dog ... learn something new everyday. I never knew you could use a regex in place of ref.

Re: Joining if an array
by Fletch (Bishop) on May 01, 2009 at 15:38 UTC

    If you're asking about joining if the value in $in is an array reference then something like my $in = ref($scalar) eq 'ARRAY' ? join( ',', @{$scalar} ) : $scalar; but there's a mild hint of XY problem in the air.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      "XY problem in the air" applies to 99% of all my posts. I am going to have that etched on my tombstone. "Here lies Rodster, he shouldn't be here though; it's an XY thing" ;)
Re: Joining if an array
by Mutant (Priest) on May 01, 2009 at 15:37 UTC
    I might be tempted to use the ternery operator, but usually for a non-trivial assignment (like a join) I'd avoid it as it just gets messy. I'd probably do:
    my $in; if (ref $scalar eq 'ARRAY') { $in = join(",",@$scalar); } else { $in = $scalar; }
    Not short, but pretty clear in what I mean (hopefully).
Re: Joining if an array
by starX (Chaplain) on May 01, 2009 at 16:01 UTC
    Kind of off topic, maybe, but I think that, in general, it's better to write code that's more readable to more people than to write code that's prettier and more concise. Especially if you think you might have to come back to it a few months after writing it. I always hate having to ask others what my code means :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-25 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found