Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Array splitting

by Kenosis (Priest)
on Oct 31, 2012 at 21:42 UTC ( [id://1001749]=note: print w/replies, xml ) Need Help??


in reply to Array splitting

You need to use eq not ==,as the latter's used for numeric comparisons:

if(substr($_,0,1) eq 'A') { ...

Output:

Ape added to A Arg added to A Beep added to trash! Circus added to trash! Ape, Arg Beep, Circus

Another option is to use a regex to check the beginning character:

if(/^A/) { ...

Also, consider doing the following for your print statements:

... print "$_ added to A\n"; ... print "$_ added to trash!\n";

Replies are listed 'Best First'.
Re^2: Array splitting
by bimleshsharma (Beadle) on Nov 01, 2012 at 08:05 UTC

    here is quick solution. See if it is useful for you.

    sub split_array { my @not_sorted = qw(Ape Arg Beep Circus); my @sorted = sort {$a cmp $b} @not_sorted; my @A = (); my @trash = (); map{$_ =~/^A.*/ ? push(@A,$_):push(@trash,$_)} @sorted; print "\n",@A,"\n"; print "\n",@trash,"\n"; }

Log In?
Username:
Password:

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

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

    No recent polls found