Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to set hash values to '-' .

by Sami_R (Sexton)
on Jun 14, 2020 at 09:46 UTC ( [id://11118040]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks

Preparing a hash, based on a condition need to set hash values to '-', is there any simplest way of implementing this, please.

For example: have only two values for day 1 and 4. And don't have values for day 2 and 3 so need to set '-'.

HASH NOW: =====> $VAR1 = { 'day' => 1, 'Balance' => '183.57', 'payment' => 0, 'Total' => 0, }; HASH NOW: =====> $VAR1 = { 'day' => 4, 'Balance' => '12.86', 'payment' => 0, 'Total' => 0, };

Expected output:

HASH NOW: =====> $VAR1 = { 'day' => 1, 'Balance' => '183.57', 'payment' => 0, 'Total' => 0, }; HASH NOW: =====> $VAR1 = { 'day' => 2, 'Balance' => '-', 'payment' => '-', 'Total' => '-', }; HASH NOW: =====> $VAR1 = { 'day' => 3, 'Balance' => '-', 'payment' => '-', 'Total' => '-', }; HASH NOW: =====> $VAR1 = { 'day' => 4, 'Balance' => '12.86', 'payment' => 0, 'Total' => 0, };

Thanks in advance.

Replies are listed 'Best First'.
Re: How to set hash values to '-' .
by dsheroh (Monsignor) on Jun 14, 2020 at 11:04 UTC
    You forgot to mention how the hash is being created and how it's being output in the first place. If you want the '-' values to actually be in the hash, then how it's created is very significant to being able to answer the question, and if you don't want them to actually be in the hash, then how it's output is equally significant.

    But I guess there's a simple (if verbose) way to add them, regardless of how it was created, because the right side of a hash assignment is really just a list, and values later in the list supersede those earlier in the list:

    %the_hash = ( day => '-', Balance => '-', payment => '-', Total => '-', %the_hash );
    This will preserve any existing values in the hash, while adding them for any keys that are missing:
    $ perl -MData::Printer -E "%h = (day => 1, Total => 2); %h = (day => ' +-', Balance => '-', payment => '-', Total => '-', %h); p %h;" { Balance "-", day 1, payment "-", Total 2 }
    And you should really standardize on either starting all the keys with lowercase or all with uppercase. Having them mixed is going to cause you (and anyone else who works on this code) all kinds of headaches when you try to access the keys 'Day', 'balance', 'Payment', or 'total'.
Re: How to set hash values to '-' .
by tybalt89 (Monsignor) on Jun 14, 2020 at 21:42 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11118040 use warnings; my %hash = ( 1 => { Balance => 183.57, day => 1, payment => 0, Total => 0 }, 4 => { Balance => 12.86, day => 4, payment => 0, Total => 0 }, ); use Data::Dump 'dd'; dd \%hash; for my $day ( 1 .. 4 ) { $hash{$day} = { Balance => '-', day => $day, payment => '-', Total = +> '-' , %{ $hash{$day} // {} } }; } use Data::Dump 'dd'; dd \%hash;

    Outputs:

    { 1 => { Balance => 183.57, day => 1, payment => 0, Total => 0 }, 4 => { Balance => 12.86, day => 4, payment => 0, Total => 0 }, } { 1 => { Balance => 183.57, day => 1, payment => 0, Total => 0 }, 2 => { Balance => "-", day => 2, payment => "-", Total => "-" }, 3 => { Balance => "-", day => 3, payment => "-", Total => "-" }, 4 => { Balance => 12.86, day => 4, payment => 0, Total => 0 }, }
Re: How to set hash values to '-' . (updated)
by AnomalousMonk (Archbishop) on Jun 14, 2020 at 10:36 UTC

    It looks as if you might be starting with an array of hashes (an AoH) and you want to add some elements (additional hashes) after the first element of the existing array. If so, one way:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = ( { 'day' => 1, 'Balance' => '183.57', 'payment' => 0, 'Total' => 0, +}, { 'day' => 4, 'Balance' => '12.86', 'payment' => 0, 'Total' => 0, +}, ); dd \@ra; ;; my @add_after_first = map { { 'day' => $_, qw(Balance - payment - Total -) } } 2, 3; ;; splice @ra, 1, 0, @add_after_first; dd \@ra; " [ { Balance => "183.57", Total => 0, day => 1, payment => 0 }, { Balance => "12.86", Total => 0, day => 4, payment => 0 }, ] [ { Balance => "183.57", Total => 0, day => 1, payment => 0 }, { Balance => "-", Total => "-", day => 2, payment => "-" }, { Balance => "-", Total => "-", day => 3, payment => "-" }, { Balance => "12.86", Total => 0, day => 4, payment => 0 }, ]
    See splice, map. Also, please see Short, Self-Contained, Correct Example.

    Update: A variation. Factoring out the null items list into the  NULL_ITEMS constant may be useful in other situations in your code. (Update: Oops... I just noticed that the "null" value I'm using in this example is, for some reason, a  ' (single-quote) instead of a  - (hyphen). Oh, well. I think you can get the idea, so I'm not going to change the code.)

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use constant NULL_ITEMS => map { $_ => '\'' } qw(Balance Total day pa +yment); ;; my @ra = ( { 'day' => 1, 'Balance' => '183.57', 'payment' => 0, 'Total' => 0, +}, { 'day' => 4, 'Balance' => '12.86', 'payment' => 0, 'Total' => 0, +}, ); dd \@ra; ;; splice @ra, 1, 0, map { { NULL_ITEMS, 'day' => $_ } } 2, 3; dd \@ra; " [ { Balance => "183.57", Total => 0, day => 1, payment => 0 }, { Balance => "12.86", Total => 0, day => 4, payment => 0 }, ] [ { Balance => "183.57", Total => 0, day => 1, payment => 0 }, { Balance => "'", Total => "'", day => 2, payment => "'" }, { Balance => "'", Total => "'", day => 3, payment => "'" }, { Balance => "12.86", Total => 0, day => 4, payment => 0 }, ]
    Also, I agree with dsheroh here that critical details of your application are missing and we are left guessing.


    Give a man a fish:  <%-{-{-{-<

Re: How to set hash values to '-' .
by kcott (Archbishop) on Jun 14, 2020 at 21:11 UTC

    G'day Sami_R,

    "... set hash values to '-', is there any simplest way of implementing this, please."

    You can assign to multiple keys at the same time using a hash slice (see "perldata: Slices"). You can assign multiple, identical values using the repetition operator (see "perlop: Multiplicative Operators").

    As already pointed out, information from you is missing. The following code should give you an idea of a technique you might employ; of course, it's impossible to even suggest implementation details.

    $ perl -e ' my @hashes; my @days = ([1,2,3], [], [], [4,5,6]); for (0 .. $#days) { my %x = (day => 1+$_); @x{qw{bal pay tot}} = @{$days[$_]} ? @{$days[$_]} : ("-") x 3; push @hashes, \%x; } use Data::Dump; dd \@hashes; ' [ { bal => 1, day => 1, pay => 2, tot => 3 }, { bal => "-", day => 2, pay => "-", tot => "-" }, { bal => "-", day => 3, pay => "-", tot => "-" }, { bal => 4, day => 4, pay => 5, tot => 6 }, ]

    — Ken

Re: How to set hash values to '-' .
by Marshall (Canon) on Jun 14, 2020 at 23:58 UTC
    Another approach is to not modify your DB with the missing day values, but rather implement a simple "lookup" or "search" function that returns the null values if a particular day value does not exist. This is one of many possibilities:
    use strict; use warnings; my $nullRecord_ref = {'Balance' => '-', 'payment' => '-', 'Total' => ' +-' }; my %hash_of_days = ( 'day1' => {'Balance' => '183.57', 'payment' => 0, 'Total' => 0, }, 'day4' => {'Balance' => '12.86', 'payment' => 0, 'Total' => 0, }, ); foreach my $day (1..5,123) { my %result = searchDB (\%hash_of_days, $day); print "Day = $day:\n"; print " ",$_ => " ".$result{$_},"\n" foreach (keys %result); } sub searchDB { my($DBref, $day) = @_; my %result; %result = %$nullRecord_ref; %result = %{$DBref->{"day$day"}} if exists $DBref->{"day$day"}; return %result; } __END__ Prints: Day = 1: Balance 183.57 payment 0 Total 0 Day = 2: payment - Total - Balance - Day = 3: Total - payment - Balance - Day = 4: payment 0 Total 0 Balance 12.86 Day = 5: Balance - payment - Total - Day = 123: Total - payment - Balance -

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-16 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found