another doggone newbie question about hash fiddling...

This is a multipart message in MIME format.
--===============0743891982==
Content-Type: multipart/alternative;
boundary="=_alternative 006D8AF0862573A7_="

This is a multipart message in MIME format.
--=_alternative 006D8AF0862573A7_=
Content-Type: text/plain; charset="US-ASCII"

Gurus,

I'm embarrassed to have to ask this, but my books don't mention it, and
neither does perldoc. I'm looking for a way to do a substitution to all
values of a hash in one line, something using, oh, grep, or map... What I
want to do is change the values of this hash:

my %foo = ( '01' => 'abc/SID/def',
'02' => 'bcd/SID/fgh',
'03' => 'hjk/SID/opq' );

to

$foo{'01'} = 'abc/12345/def';
$foo{'02'} = 'bcd/12345/fgh';
$foo{'03'} = 'hjk/12345/opq';

In other words, I want to replace the text 'SID' in each value of the hash
with the text '12345' using one line of code. Can this be done?

Thanks,

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
847-914-5150

Let us permit nature to have her way; she understands her business better
than we do. -- Michel de Montaigne
--=_alternative 006D8AF0862573A7_=
Content-Type: text/html; charset="US-ASCII"


<br><font size=2 face="sans-serif">Gurus,</font>
<br>
<br><font size=2 face="sans-serif">I'm embarrassed to have to ask this,
but my books don't mention it, and neither does perldoc.  I'm looking
for a way to do a substitution to all values of a hash in one line, something
using, oh, grep, or map...  What I want to do is change the values
of this hash:</font>
<br>
<br><font size=2 face="sans-serif">my %foo = ( '01' => 'abc/SID/def',</font>
<br><font size=2 face="sans-serif">           
            '02' =>  'bcd/SID/fgh',</font>
<br><font size=2 face="sans-serif">           
            '03' => 'hjk/SID/opq' );</font>
<br>
<br><font size=2 face="sans-serif">to </font>
<br>
<br><font size=2 face="sans-serif">$foo{'01'} = 'abc/12345/def';</font>
<br><font size=2 face="sans-serif">$foo{'02'} = 'bcd/12345/fgh';</font>
<br><font size=2 face="sans-serif">$foo{'03'} = 'hjk/12345/opq';</font>
<br>
<br><font size=2 face="sans-serif">In other words, I want to replace the
text 'SID' in each value of the hash with the text '12345' using one line
of code.  Can this be done?</font>
<br>
<br><font size=2 face="sans-serif">Thanks,</font>
<br>
<br><font size=2 face="sans-serif">Deane Rothenmaier<br>
Programmer/Analyst<br>
Walgreens Corp.<br>
847-914-5150<br>
<br>
Let us permit nature to have her way; she understands her business better
than we do. -- Michel de Montaigne</font>
--=_alternative 006D8AF0862573A7_=--


--===============0743891982==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0743891982==--
Deane.Rothenmaier [ Di, 04 Dezember 2007 20:56 ] [ ID #1885735 ]

Re: another doggone newbie question about hash fiddling...

This is a multipart message in MIME format.
--===============1177815806==
Content-Type: multipart/alternative;
boundary="=_alternative 00723714862573A7_="

This is a multipart message in MIME format.
--=_alternative 00723714862573A7_=
Content-Type: text/plain; charset="US-ASCII"

William,

That's what I was looking for! Thanks a heap!!

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
847-914-5150

Let us permit nature to have her way; she understands her business better
than we do. -- Michel de Montaigne
--=_alternative 00723714862573A7_=
Content-Type: text/html; charset="US-ASCII"


<br><font size=2 face="sans-serif">William,</font>
<br>
<br><font size=2 face="sans-serif">That's what I was looking for!  Thanks
a heap!!</font>
<br>
<br><font size=2 face="sans-serif">Deane Rothenmaier<br>
Programmer/Analyst<br>
Walgreens Corp.<br>
847-914-5150<br>
<br>
Let us permit nature to have her way; she understands her business better
than we do. -- Michel de Montaigne</font>
--=_alternative 00723714862573A7_=--


--===============1177815806==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1177815806==--
Deane.Rothenmaier [ Di, 04 Dezember 2007 21:47 ] [ ID #1885736 ]

RE: another doggone newbie question about hash fiddling...

This is a multipart message in MIME format.

--===============0615181842==
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0180_01C83673.EA4D42D0"
Content-Language: en-ca

This is a multipart message in MIME format.

------=_NextPart_000_0180_01C83673.EA4D42D0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

You said: "I want to replace the text 'SID' in each value of the hash with the text '12345'".



English to perl translation:



s/SID/12345/ for values %foo;



Cheers,

-Jan



From: activeperl-bounces [at] listserv.ActiveState.com [mailto:activeperl-bounces [at] listserv.ActiveState.com] On Behalf Of
Deane.Rothenmaier [at] walgreens.com
Sent: December 4, 2007 11:56 AM
To: activeperl [at] listserv.ActiveState.com
Subject: another doggone newbie question about hash fiddling...




Gurus,

I'm embarrassed to have to ask this, but my books don't mention it, and neither does perldoc. I'm looking for a way to do a
substitution to all values of a hash in one line, something using, oh, grep, or map... What I want to do is change the values of
this hash:

my %foo = ( '01' => 'abc/SID/def',
'02' => 'bcd/SID/fgh',
'03' => 'hjk/SID/opq' );

to

$foo{'01'} = 'abc/12345/def';
$foo{'02'} = 'bcd/12345/fgh';
$foo{'03'} = 'hjk/12345/opq';

In other words, I want to replace the text 'SID' in each value of the hash with the text '12345' using one line of code. Can this
be done?

Thanks,

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
847-914-5150

Let us permit nature to have her way; she understands her business better than we do. -- Michel de Montaigne


------=_NextPart_000_0180_01C83673.EA4D42D0
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" =
xmlns:o=3D"urn:schemas-microsoft-com:office:office" =
xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" =
xmlns=3D"http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dus-ascii">
<meta name=3DGenerator content=3D"Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
[at] font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
[at] font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
[at] font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
[at] font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
..MsoChpDefault
{mso-style-type:export-only;}
[at] page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
{page:Section1;}
/* List Definitions */
[at] list l0
{mso-list-id:1824421968;
mso-list-type:hybrid;
mso-list-template-ids:987379888 1440256456 67698691 67698693 67698689 =
67698691 67698693 67698689 67698691 67698693;}
[at] list l0:level1
{mso-level-start-at:0;
mso-level-number-format:bullet;
mso-level-text:\F0D8;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;
mso-fareast-font-family:Calibri;
mso-bidi-font-family:"Times New Roman";}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>

<body lang=3DEN-US link=3Dblue vlink=3Dpurple>

<div class=3DSection1>

<p class=3DMsoNormal><span =
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>You said: “I want to replace the text 'SID' in each =
value
of the hash with the text '12345'”.<o:p></o:p></span></p>

<p class=3DMsoNormal><span =
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'><o:p> </o:p></span></p>

<p class=3DMsoNormal><span =
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>English to perl translation:<o:p></o:p></span></p>

<p class=3DMsoNormal><span =
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'><o:p> </o:p></span></p>

<p class=3DMsoNormal style=3D'text-indent:5.25pt'><span =
style=3D'font-size:11.0pt;
font-family:"Courier New"'>s/SID/12345/ for values =
%foo;<o:p></o:p></span></p>

<p class=3DMsoNormal><span =
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'><o:p> </o:p></span></p>

<p class=3DMsoNormal><span =
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>Cheers,<o:p></o:p></span></p>

<p class=3DMsoNormal><span =
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>-Jan<o:p></o:p></span></p>

<p class=3DMsoNormal><a name=3D"_MailEndCompose"><span =
style=3D'font-size:11.0pt;
font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span=
></a></p>

<div style=3D'border:none;border-left:solid blue 1.5pt;padding:0cm 0cm =
0cm 4.0pt'>

<div>

<div style=3D'border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt =
0cm 0cm 0cm'>

<p class=3DMsoNormal><b><span =
style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"' >From:</span>=
</b><span
style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"' >
activeperl-bounces [at] listserv.ActiveState.com
[mailto:activeperl-bounces [at] listserv.ActiveState.com] <b>On Behalf Of =
</b>Deane.Rothenmaier [at] walgreens.com<br>
<b>Sent:</b> December 4, 2007 11:56 AM<br>
<b>To:</b> activeperl [at] listserv.ActiveState.com<br>
<b>Subject:</b> another doggone newbie question about hash =
fiddling...<o:p></o:p></span></p>

</div>

</div>

<p class=3DMsoNormal><o:p> </o:p></p>

<p class=3DMsoNormal><br>
<span =
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> Gurus,</span>=
<br>
<br>
<span style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'>I'm =
embarrassed
to have to ask this, but my books don't mention it, and neither does =
perldoc.
 I'm looking for a way to do a substitution to all values of a hash =
in one
line, something using, oh, grep, or map...  What I want to do is =
change
the values of this hash:</span> <br>
<br>
<span style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'>my =
%foo =3D (
'01' =3D> 'abc/SID/def',</span> <br>
<span style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'>  =
 
                    =
'02'
=3D>  'bcd/SID/fgh',</span> <br>
<span style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'>  =
 
                    =
'03'
=3D> 'hjk/SID/opq' );</span> <br>
<br>
<span style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'>to =
</span><br>
<br>
<span =
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> $foo{'01'} =
=3D
'abc/12345/def';</span> <br>
<span =
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> $foo{'02'} =
=3D
'bcd/12345/fgh';</span> <br>
<span =
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> $foo{'03'} =
=3D
'hjk/12345/opq';</span> <br>
<br>
<span style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'>In =
other words,
I want to replace the text 'SID' in each value of the hash with the text
'12345' using one line of code.  Can this be done?</span> <br>
<br>
<span =
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> Thanks,</span=
> <br>
<br>
<span style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'>Deane
Rothenmaier<br>
Programmer/Analyst<br>
Walgreens Corp.<br>
847-914-5150<br>
<br>
Let us permit nature to have her way; she understands her business =
better than
we do. -- Michel de Montaigne</span><o:p></o:p></p>

</div>

</div>

</body>

</html>

------=_NextPart_000_0180_01C83673.EA4D42D0--


--===============0615181842==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0615181842==--
Jan Dubois [ Di, 04 Dezember 2007 21:48 ] [ ID #1885737 ]

Re: another doggone newbie question about hash fiddling...

my %foo = ( '01' => 'abc/SID/def',
'02' => 'bcd/SID/fgh',
'03' => 'hjk/SID/opq' );

to

$foo{'01'} = 'abc/12345/def';
$foo{'02'} = 'bcd/12345/fgh';
$foo{'03'} = 'hjk/12345/opq';


$foo{$_} =~ s/SID/12345/ for keys %foo;

a

Andy Bach
Systems Mangler
Internet: andy_bach [at] wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932

Remember, the first rule of optimisation is: don't do it yet. :-) Brian
Raven

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Andy_Bach [ Di, 04 Dezember 2007 22:14 ] [ ID #1885738 ]

RE: another doggone newbie question about hash fiddling...

> English to perl translation:

s/SID/12345/ for values %foo;


Nice - this takes advantage of the fact that $_ here is a reference to the
actual value of the hash so that the subst. works directly on the hash.
The same fact works for arrays too
[at] foo = ('abc/SID/def', 'bcd/SID/fgh', 'hjk/SID/opq' );

foreach ( [at] foo ) {
s/SID/12345/;
}

or:
s/SID/12345/ for [at] foo;

It's also a cautionary tale - if you're using the default "$_" in for
loops, you can munge your original data.
foreach my $foo ( [at] foo ) {
$foo =~ s/SID/12345/;
}

this doesn't mess w/ [at] foo.

a

Andy Bach
Systems Mangler
Internet: andy_bach [at] wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932

Remember, the first rule of optimisation is: don't do it yet. :-) Brian
Raven

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Andy_Bach [ Di, 04 Dezember 2007 22:20 ] [ ID #1885739 ]

RE: another doggone newbie question about hash fiddling...

On Tue, 04 Dec 2007, Andy_Bach [at] wiwb.uscourts.gov wrote:
> It's also a cautionary tale - if you're using the default "$_" in for
> loops, you can munge your original data.
> foreach my $foo ( [at] foo ) {
> $foo =~ s/SID/12345/;
> }
>
> this doesn't mess w/ [at] foo.

That is not correct; it will still modify [at] foo. It only protects
you against functions that you might call inside of your for() loop
from messing with your values via $_, which is a global variable
in Perl 5.8.

Note that you can have a lexical $_ in 5.10:

foreach my $_ ( [at] foo ) {
s/SID/12345/;
}

Cheers,
-Jan

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Jan Dubois [ Di, 04 Dezember 2007 22:41 ] [ ID #1885740 ]

RE: another doggone newbie question about hash fiddling...

--------------8<---------------
Note that you can have a lexical $_ in 5.10:

foreach my $_ ( [at] foo ) {
s/SID/12345/;
}
--------------8<---------------

Arrrrgggghhhh! Why is that considered a good feature to add to 5.10?

Sure scoping and all, but do the other perlvars behave the same? I kind of like the local rule of thumb for perlvar . . .

Just in

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Justin Allegakoen [ Mi, 05 Dezember 2007 03:20 ] [ ID #1886681 ]

RE: another doggone newbie question about hash fiddling...

--------------8<---------------
Note that you can have a lexical $_ in 5.10:

foreach my $_ ( [at] foo ) {
s/SID/12345/;
}
--------------8<---------------

Arrrrgggghhhh! Why is that considered a good feature to add to 5.10?

Sure scoping and all, but do the other perlvars behave the same? I kind of like the local rule of thumb for perlvar . . .

Just in

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Justin Allegakoen [ Mi, 05 Dezember 2007 03:20 ] [ ID #1886682 ]

RE: another doggone newbie question about hash fiddling...

On Tue, 04 Dec 2007, Justin Allegakoen wrote:
> --------------8<---------------
> Note that you can have a lexical $_ in 5.10:
>
> foreach my $_ ( [at] foo ) {
> s/SID/12345/;
> }
> --------------8<---------------
>
> Arrrrgggghhhh! Why is that considered a good feature to add to 5.10?
>
> Sure scoping and all, but do the other perlvars behave the same? I
> kind of like the local rule of thumb for perlvar . . .

This is special-cased for $_. From perlvar.pod:

| As C<$_> is a global variable, this may lead in some cases to unwanted
| side-effects. As of perl 5.9.1, you can now use a lexical version of
| C<$_> by declaring it in a file or in a block with C<my>. Moreover,
| declaring C<our $_> restores the global C<$_> in the current scope.

Cheers,
-Jan

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Jan Dubois [ Mi, 05 Dezember 2007 05:04 ] [ ID #1886683 ]
Perl » gmane.comp.lang.perl.active-perl » another doggone newbie question about hash fiddling...

Vorheriges Thema: I see
Nächstes Thema: "Quality Percentage" of hashes as reported by Devel::Peek