Results 1 to 4 of 4

Thread: Minor problem with perl

  1. #1
    Join Date
    Dec 2007
    Location
    ~
    Beans
    314
    Distro
    Ubuntu 10.04 Lucid Lynx

    Exclamation Minor problem with perl

    Hi,
    I have an hash in perl of the form
    Code:
    %hash = {
      val1 => ["string",1],
      val2 => ["string",10],
      val3 => ["string",100],
      val4 => ["string",1000],
      val5 => ["string",10000],
      val6 => ["string",100000],
    };
    How do I change the integral values to zero in each line using a map statement.
    Right now I am naively looping through the keys of the hash.
    I guess there must be a smaller way to do this.

    Thx for the help!!

  2. #2
    Join Date
    Mar 2006
    Beans
    393

    Re: Minor problem with perl

    Do you mean this:
    Code:
    %hash = (
      val1 => ["string",1],
      val2 => ["string",10],
      val3 => ["string",100],
      val4 => ["string",1000],
      val5 => ["string",10000],
      val6 => ["string",100000],
    );
    Or this?
    Code:
    $hash = {
      val1 => ["string",1],
      val2 => ["string",10],
      val3 => ["string",100],
      val4 => ["string",1000],
      val5 => ["string",10000],
      val6 => ["string",100000],
    };
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication as it is about coding.

  3. #3
    Join Date
    Dec 2007
    Location
    ~
    Beans
    314
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Minor problem with perl

    the one with %

  4. #4
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Minor problem with perl

    "map" returns a list that will go unused, so it's not the right tool here. "foreach" can do it in a one-liner:
    Code:
    $hash{$_}->[1] = 0 foreach keys %hash;
    Here's another way, just using the values since they are references:
    Code:
    $_->[1] = 0 foreach values %hash;
    Last edited by gmargo; October 18th, 2010 at 06:48 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •