Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

(Golf!) Re^2: better way to convert a string into an array and an hash

by dragonchild (Archbishop)
on Oct 05, 2004 at 15:31 UTC ( [id://396622]=note: print w/replies, xml ) Need Help??


in reply to Re: better way to convert a string into an array and an hash
in thread better way to convert a string into an array and an hash

my (@a, %h); $_ = '1:1,2:1,3:2,500:2,505:1'; #234567890#234567890#234567890#234567890#234567890#234567890 # 47 characters ... %h=map{@@=/[^:]+/g;$a[$#a+1]=$@[0];@@}/[^,]+/g; # 44 ... %h=map{@@=/[^:]+/g;push@a,$@[0];@@}/[^,]+/g; # Based on duff's whimsy above ... 42 characters %h=@a=/[^:,]+/g;@a=@a[grep!($_%2),0..$#a]; # Further whimsy ... 29 characters %h=/[^:,]+/g;@a=/([^,:]+):/g;

Rules:

  • You can assume all the code above the counting line.
  • Bonus points if you run under strict and warnings.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

  • Comment on (Golf!) Re^2: better way to convert a string into an array and an hash
  • Download Code

Replies are listed 'Best First'.
Re^3: better way to convert a string into an array and an hash
by fergal (Chaplain) on Oct 05, 2004 at 15:45 UTC
    If you convert my answer to use short variable names and remove the ?: you get 44 relevant chars
    #234567890#234567890#234567890#234567890#234567890#234567890 s/(\d+):(\d+)(,|$)/$h{$1}=$2;push(@a,$1)/ge;
Re^3: better way to convert a string into an array and an hash
by duff (Parson) on Oct 05, 2004 at 15:50 UTC

    Surely, %h=/\d+/g;@a=/(\d+):/g; would be closer to what's wanted :)

Re^3: better way to convert a string into an array and an hash
by Rhose (Priest) on Oct 05, 2004 at 16:08 UTC
    I'm not a golfer, but...

    tr/:/,/;%h=eval;@a=keys%h;

    Update

    y/:/,/;%h=eval;@a=keys%h;

    Update 2

    If the ordering of @a is important, how about

    @a=/(\d+):/g;y/:/,/;%h=eval;

      This doesn't preserve the order of the keys. That's a problem.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re^3: better way to convert a string into an array and an hash
by Miguel (Friar) on Oct 05, 2004 at 15:45 UTC
    GEEES!!!

    Thank you all for showing me that I still have a lot to learn! :D

    Miguel

Log In?
Username:
Password:

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

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

    No recent polls found