On Apr 3, 2:31 pm, crypt...@gmail.com (Wernher Eksteen) wrote:
> > I suggest you reduce your tab size from eight characters, which leave
> > your code spread out and less readable. Four or two is more usual
> > nowadays.
>
> Thank you, I will do so from now on.
>
> > Meaningful variable names are also important. Using $i as the key to
> > %hash1 and $b as the key to %hash2 is a very bad idea: both have
> > different conventional uses and $b is a special variable used internally
> > by Perl.
>
> Thanks you, I will add more descriptive names in future. I didn't know $b
> is a special internal variable used by Perl.
>
> > Long chains of concatenated strings can be improved visually by
> > interpolation:
>
> >  push @matched, "$i $hash1{$i} $b $hash2{$b}\n";
>
> > or formatting:
>
> >  push @matched, sprintf "%s %s %s %s\n", $i, $hash1{$i}, $b, $hash2{$b};
>
> Wow, thanks!
>
> > As for an improved version, your main purpose seems to be to combine
> > data that belongs under the same /emcpower.*/ prefix. The program below
> > does this by modifying %hash1, rather than building two new arrays as
> > yours does. If this is unacceptable then come back to us.
>
> This is acceptable, thank you!
>
> > The program iterates over the records in %hash2, removes the decimals
> > from the end of the key, and uses the result to select the record in
> > %hash1 that should be appended to.
>
> Thank you, I definitely still need to familiarize myself better with
> hashes and I
> am working on getting there. From the likes of you and others in this
> list I will
> achieve this goal much quicker than just by myself.
>
> > HTH,
>
> Yes, it does very much, thank you!
>
>
>
>
>
> > Rob
>

{snip the code I will slightly alter to meet your description}



> This is really totally awesome, thank you so very much!
>
> I really like what you have done with this line, it's very cool!
>
> emcpowerbc sdb sdbe sddh sdfk emcpowerbc1 /s00_11 emcpowerbc2 /utl_file_dir
>
> But, I plan to add things like "disk size",  "disk size used" and
> "disk size free" numeric values
> next to each partition mount ( emcpowerbc1 /s00_11 ) and (emcpowerbc2
> /utl_file_dir) in this case,
> so I would prefer to have it like this instead:
>
> emcpowerbc sdb sdbe sddh sdfk emcpowerbc1 /s00_11
>                                                emcpowerbc2 /utl_file_dir
>
> or
>
> emcpowerbc sdb sdbe sddh sdfk emcpowerbc1 /s00_11
> emcpowerbc sdb sdbe sddh sdfk emcpowerbc2 /utl_file_dir
>
> Could you please show me how to change your excellent code to achieve
> either one of these instead?
>
> Many thanks!
> Wernher- Hide quoted text -
>
> - Show quoted text -

Hello Wernher,

Rob's code could be reconfigured slightly to do so, but requires a
different approach.
I will paste it below.

One change I made was to make the *values* of %hash1 an array ref as I
thought this was how the hash was originally constructed. If the value
for %hash1 was a string as in Rob's hash, the code below could be
altered slightly to perform the task as well.

You said 'But, I plan to add things like "disk size",  "disk size
used" and
"disk size free" numeric values next to each partition mount '

It is good to build up a program in small steps and test as you go,
but it is also helpful to have a large view of the problem so that you
won't create data structures that work for the first stages of the
program but fail to accomodate later alterations. You really kinda
need to have the big picture at the start.

Chris


#!/usr/bin/perl
use strict;
use warnings;

# I changed the value of the hash to an array (ref). Rob had a string
here
my %hash1 = (
  emcpowera  => [qw/sdbd sddg sdfj sdhm/],
  emcpoweraa => [qw/sdae sdch sdek sdgn/],
  emcpowerbc => [qw/sdb sdbe sddh sdfk/],
  emcpowerc  => [qw/sdbb sdde sdfh sdhk/],
  emcpowerd  => [qw/sdba sddd sdfg sdhj/],
  emcpowerz  => [qw/sdba sddd sdfg sdhj/]
);


my %hash2 = (
  emcpowera1  => "/dwpdb006",
  emcpoweraa1 => "/dwpdb033",
  emcpowerbc1 => "/s00_11",
  emcpowerbc2 => "/utl_file_dir",
  emcpowerc1  => "/odsdb006",
  emcpowerd1  => "/odsdb005",
);

my %seen;
for my $key (sort keys %hash2) {
        (my $k1 = $key) =~ s/\d+\z//;
        if (my $aref = $hash1{ $k1 }) {

                my $string = "$k1 @$aref";
                $string = ' ' x length($string) if $seen{$k1}++;

                print "$string\t$key $hash2{$key}\n";
        }
}

delete @hash1{ keys %seen };
print "$_ @{ $hash1{$_} }\n" for sort keys %hash1;


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to