Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

how to use join my array ?

by manan.patel (Initiate)
on May 01, 2014 at 09:58 UTC ( #1084595=perlquestion: print w/replies, xml ) Need Help??

manan.patel has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, i have a hash and from that i access the key values(string) from it. i want to insert a "-" at starting of each and every value(string).
foreach $z ( %abc) { push (@ab, $z}); $ab = join "\-",@ab; }
suppose foreach interative $z contain (to) than (from) like that way. now i want to store that scaler value in to one array called @ab. i want to store that scaler value looks like in array is ( -to -from)... please help me for this.. Thanks for tour time

Replies are listed 'Best First'.
Re: how to use join my array ?
by Bloodnok (Vicar) on May 01, 2014 at 10:22 UTC
    I'm not sure that I fully understand the question, but ... is this something like what you're after-
    foreach my $z ( %abc) { push @ab, "-$z"; }
    or possibly @ab = map { "-$_" } @ab; ??

    foreach my $z (keys %abc) { push @ab, "-$z"; }
    or possibly @ab = map { "-$_" } keys %abc; ??

    Update 1:

    In fact I misunderstood the question so much as to provide complete and utter drivel as a reply - doh!!!

    Update 2:

    Attempted to provide a more meaningful and possibly half-sensible, reply.

    A user level that continues to overstate my experience :-))
Re: how to use join my array ?
by rnewsham (Curate) on May 01, 2014 at 10:24 UTC

    It is not clear exactly what you are trying to achieve. It looks like you want to create an array @ab that contains just the keys from the hash %abc, each with a hypen prepended.

    use strict; use warnings; my %abc = ( to => 'def', from => 'ghi', ); my @ab; for ( keys %abc ) { push @ab, "-$_"; } print "$_\n" for @ab; #Output -to -from

    Will do that, or you can use map to do the same thing.

    use strict; use warnings; my %abc = ( to => 'def', from => 'ghi', ); my @ab = map { "-$_" } keys %abc; print "$_\n" for @ab;

    As doing this disconnects the modified keys from their previously associated values, I guess this is not really what you want. I think it is more likely that you want to just modify the keys in the hash, or more simply create a new hash with the modified keys.

    my %ab = map { '-' .$_ => $abc{$_} } keys %abc;
Re: how to use join my array ?
by sudevshetty (Initiate) on May 01, 2014 at 10:21 UTC
    while( my($key, $value) = each(%abc)){ $abc{$key}="-$value"; }
Re: how to use join my array ?
by Laurent_R (Canon) on May 01, 2014 at 17:02 UTC
Re: how to use join my array ?
by sudevshetty (Initiate) on May 01, 2014 at 10:18 UTC
    try this while( my($key, $value) = each(%abc)){ $Hash{$key}="-$value"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2021-01-15 15:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?