Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

combine arrays, duh!

by dbs (Sexton)
on Apr 19, 2011 at 20:49 UTC ( #900219=perlquestion: print w/replies, xml ) Need Help??

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

How can I remove all these extra spaces, special chars. etc, from my 1st array below? I want the arrays to look the same in terms of Dumper. Please help! I tried

 my @E = grep { !/^\n*$/ } @EuserADgrps;

AD grps 1st 6 $VAR1 = [ 'cbpro_group ', 'cbproop ', 'cbqa ', 'cbqaop ', 'cbtest ', 'cbtestop ' ]; $VAR2 = ' '; $VAR1 = [ 'cbpro', 'cbproop', 'cbqa', 'cbqaop', 'cbtest', 'cbtestop' ]; $VAR2 = ' '; 6

Replies are listed 'Best First'.
Re: combine arrays, duh!
by MidLifeXis (Monsignor) on Apr 19, 2011 at 20:57 UTC

    See chomp for a way to do it.

    --MidLifeXis

Re: combine arrays, duh!
by jffry (Hermit) on Apr 19, 2011 at 21:29 UTC

    I think you were looking for map not grep.

    This

    #!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my @e1 = ( ' bob', ' sue', ' joe_1', ); my @e2 = ( 'bob', 'sue', 'joe1', ); my @f = map { s/\W|_//g; $_ } @e1; print Dumper(\@f); print Dumper(\@e2);

    Gives this output:

    $VAR1 = [ 'bob', 'sue', 'joe1' ]; $VAR1 = [ 'bob', 'sue', 'joe1' ];

    Also, it might be better to put more thought into your subject line next time. That helps other monks with a similar problem find this thread easier. For example, "remove spaces from array elements" might be good

Re: combine arrays, duh!
by dasgar (Priest) on Apr 19, 2011 at 21:04 UTC

    Haven't tested it, but I believe that the code below will should strip out all of the "unwanted" characters in an array. ("Unwanted" as in non-alphanumeric, which is what I believe that you're asking for.)

    foreach my $item (@array) { $item =~ s/\W//g; # Remove non-word characters $item =~ s/_//g; # Remove all underscores }
Re: combine arrays, duh!
by Marshall (Canon) on Apr 20, 2011 at 23:45 UTC
    When you post code here, please show the actual code and the actual output. I had to guess a bit as to what you had...

    grep{} is a filtering operation. It doesn't modify anything.
    my @E = grep { !/^\n*$/ } @EuserADgrps;
    means: transfer each item from @EuserADgrps to @E which satisfies the regex within the grep{}.

    /^\n*$/ means: a string that may have zero or more \n characters, but no other characters - a pretty "rare duck"; perhaps a null string, "". When you negate that with the !, it means that for each item in @EuserADgrps, transfer it to @E if it is not one of these "rare ducks". I suspect that this grep{} does nothing to your actual data.

    Use @X=map{..blah..;$_}@Y; to transform a Y into an X. But I don't think that you need that here either.

    The chomp() function will remove trailing "new lines" from a string, or you can also use chomp() on an array. chomp(@x) will modify @x. So if you don't want that, make a copy:  @y=@x; chomp(@y);#See below:

    #!/usr/bin/perl -w use strict; use Data::Dumper; my @EuserADgrps = ("abc\n", "xyz\n"); my @E = grep { !/^\n*$/ } @EuserADgrps; #so weird it's probably wrong print Dumper \@EuserADgrps; print Dumper \@E; chomp(@EuserADgrps); #this modifies the @EuserADgrps array print "now after the chomp of ".'@EuserADgrps'." \n"; print Dumper \@EuserADgrps; __END__ printout: $VAR1 = [ 'abc ', 'xyz ' ]; $VAR1 = [ 'abc ', 'xyz ' ]; now after the chomp of @EuserADgrps $VAR1 = [ 'abc', 'xyz' ];

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2021-04-23 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?