Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to differentiate an empty array from an unitialized one?

by sundialsvc4 (Abbot)
on Jul 09, 2018 at 14:05 UTC ( #1218165=note: print w/replies, xml ) Need Help??


in reply to How to differentiate an empty array from an unitialized one?

Three examples should suffice.   (Note that in each case these are Perl one-liners intended for the command-line, so $ characters are escaped for the shell, thusly:   \$.   Only $ is seen by Perl.)

Without LIMIT, split omits trailing empty groups:

$ perl -e "use strict; use warnings; my \$a='a:b::c:d::'; my @b = spli +t(/\:/, \$a); use Data::Dumper; print Data::Dumper->Dump([\$a, \@b],[ +'a','b']);" $a = 'a:b::c:d::'; $b = [ 'a', 'b', '', 'c', 'd' ];

LIMIT=-1 causes them to be included:

$ perl -e "use strict; use warnings; my \$a='a:b::c:d::'; my @b = spli +t(/\:/, \$a, -1); use Data::Dumper; print Data::Dumper->Dump([\$a, \@ +b],['a','b']);" $a = 'a:b::c:d::'; $b = [ 'a', 'b', '', 'c', 'd', '', '' ];

Perl-5 sees adjacent commas in a list-constructor as nothing, so it produces a smaller-than-expected list which join then processes, interpreting undef as an empty string:

$ perl -e "my @a=('a','q',,undef,,'b','c',,); my \$b = join(':', @a); +use Data::Dumper; print Data::Dumper->Dump([\@a, \$b], ['a','b']);"Q $a = [ 'a', 'q', undef, 'b', 'c' ]; $b = 'a:q::b:c';

Replies are listed 'Best First'.
Re^2: How to differentiate an empty array from an unitialized one?
by Marshall (Abbot) on Jul 09, 2018 at 18:14 UTC
    I congratulate you for posting some actual Perl code!
    I do have some quibbles with it. For example, you are experienced enough to know never to use $a or $b as a user variable name since these variable names have special meaning within Perl.
    I don't want to be overly critical lest I discourage you from posting code.

    Update: Three examples should suffice. Ha!

    You did not consider blank fields at the beginning of the line.
    I don't think that any beginning Perl'er needs to know this, but:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $str =" abc xyz \n"; my @tokens = split(/\s+/,$str); print Dumper \@tokens; =PRINTS: $VAR1 = [ '', 'abc', 'xyz' ]; =cut @tokens = split (' ',$str); print Dumper \@tokens; # note: leading blank field is not there! # no need to remove spaces at beginning or end of # line with this special situation. =PRINTS: $VAR1 = [ 'abc', 'xyz' ]; =cut
Re^2: How to differentiate an empty array from an unitialized one?
by AnomalousMonk (Bishop) on Jul 09, 2018 at 20:18 UTC

    I, too, must commend your efforts to post working code examples. Long may they multiply and be fruitful! (It would have been better, however, if most of the examples given had not simply repeated examples previously given by others. The one example that does not repeat a previous example in the thread does not seem to address any point previously raised in the thread.)

    ... these are Perl one-liners intended for the command-line, so $ characters are escaped for the shell ...

    The examples you give appear to be for a *nix shell (bash?). Another way, and by far the most common for PerlMonks example code postings (or so it seems to me), is to use a non-interpolating shell quote character; see here and here for what seem to be *nix examples. Using non-interpolating quotation would seem to be a much simpler, hence preferable, way to present such Perl code.


    Give a man a fish:  <%-{-{-{-<

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1218165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2019-12-14 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?