Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Adding element to Array?

by cthar (Novice)
on Oct 13, 2009 at 05:15 UTC ( [id://800845]=perlquestion: print w/replies, xml ) Need Help??

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

How do we add element to an array only if the element is non-empty? ie.... i have a list of element:

a b c d a a c d c
... etc so i want to add only a, b, c and d, ignoring the blank spaces. i tried:
unless($list eq " ") { push(@array, $list); }
what am i doing wrong? thanks.

Again... i dont want to store/add the repeated element.... ie my array will contain only a, b , c and d. Thanks.

Replies are listed 'Best First'.
Re: Adding element to Array?
by cdarke (Prior) on Oct 13, 2009 at 08:43 UTC
    It depends on what you mean by empty.

    Your test unless($list eq " ") does not test for an empty string, but for a visible space: is that empty?

    Perl uses the scalar value of undef for "empty", although you could consider zero as being empty as well: zero and empty string equate to false.

    Not sure why other monks assumed your data was coming from a file, maybe it is but you don't say that. Interestingly, when I downloaded your data I found you had trailing spaces after the first b, c, and d. Maybe that is your problem? Here is my solution, which also removes trailing white-space:
    use warnings; use strict; my @array; while (my $list = <DATA>) { # Remove trailing white-space, no need for chomp $list =~ s/\s+$//; # If the data is coming from a file (like this), # then you don't need to test defined if (defined $list and $list) { push(@array, $list); } } print "@array\n"; __DATA__ a b c d a a c d c
Re: Adding element to Array?
by NetWallah (Canon) on Oct 13, 2009 at 05:30 UTC
    chomp is your friend.

    If the $list is EMPTY, it will only contain a linefeed character(linux), which will be removed by chomp, so the following code should work:

    chomp $list; push @array,$list if length($list) > 0;
    More experienced programmers would probably use a regular expression, looking for a "word" character before collecting it.

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Adding element to Array?
by bichonfrise74 (Vicar) on Oct 13, 2009 at 05:49 UTC
    Try something like this. You need a hash so that you do not store repeated elements.
    #!/usr/bin/perl use strict; my %record; while (my $line = <DATA>) { $record{$line}++ if ( $line =~ /\S/ ); } print for sort keys %record; __DATA__ a b c d a a c d c
Re: Adding element to Array?
by ph0enix (Friar) on Oct 13, 2009 at 07:16 UTC

    If '0' (zero) can not be a valid element, you can try following code (append list element to array if it is "true" - not empty and not '0')

    push @array, $list if $list;
Re: Adding element to Array?
by Marshall (Canon) on Oct 14, 2009 at 09:22 UTC
    I was also confused by the wording of your problem and what "element is non-empty" would mean. You also say that you don't want repeated values, so a hash is idea for eliminating those. Sounds like a fusion of bichonfrise74 and cdarke ideas is what you need.

    #!/usr/bin/perl -w use strict; my %record; while (my $line = <DATA>) { $line =~ s/\s+$//; #see [cdarke]'s comment, this #also does chomp at same time $record{$line}=1 if ( $line ne ""); #comparison allows "zero" #$record{$line}++ if ( $line ne ""); #is also ok too. But I didn't #think that you needed count } my @record = sort keys %record; #sort keys %record is a list and can just be assigned to an #array variable, which appears to be what you want as result. #note that in Perl, the identifier "record" can be used both as #an array(@record) and as a hash(%hash). print "@record"; #prints: a b c d __DATA__ a b c d a a c d c

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found