Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding ' ' around all values in hash

Tags:

string

perl

Im trying to surround all values in my hash with single quotes. here is my code.

    foreach(values(%properties_hash))
    {
            print "'".$_."'\n";
    }

Right now I'm printing. How would I actually augment the value. Also this prints

'logs
'format/systemout-2010-format.txt
'analyze

It is only printing the first '. Why would that be?

Thanks!

like image 940
PinkElephantsOnParade Avatar asked Dec 09 '25 20:12

PinkElephantsOnParade


2 Answers

Your code is working.

my %h = (
        'a' => 1,
        'b' => 2,
        'c' => 3,
        'd' => 4,
);

foreach(values(%h)) {
        print "'".$_."'\n";
}

prints

'3'
'1'
'2'
'4'

Your strings are probably ended with "\r", so the ending "'" is printed over 1 st "'" and therefore don't see the last apostrophe. try:

foreach(values(%h)) {
        s/[\r\n]//g;
        print "'".$_."'\n";
}
like image 50
jm666 Avatar answered Dec 12 '25 15:12

jm666


How about this?

foreach (keys %properties_hash) {
  $properties_hash{$_} = "'$properties_hash{$_}'\n";
}
like image 37
raina77ow Avatar answered Dec 12 '25 16:12

raina77ow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!