another doggone newbie question about hash fiddling...
another doggone newbie question about hash fiddling...
am 04.12.2007 20:56:15 von Deane.Rothenmaier
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"
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_=--
--===============0743891982==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0743891982==--
Re: another doggone newbie question about hash fiddling...
am 04.12.2007 21:47:17 von Deane.Rothenmaier
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"
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_=--
--===============1177815806==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1177815806==--
RE: another doggone newbie question about hash fiddling...
am 04.12.2007 21:48:05 von Jan Dubois
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@listserv.ActiveState.com [mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Deane.Rothenmaier@walgreens.com
Sent: December 4, 2007 11:56 AM
To: activeperl@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
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">
charset=3Dus-ascii">
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'”.
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>English to perl translation:
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>
style=3D'font-size:11.0pt;
font-family:"Courier New"'>s/SID/12345/ for values =
%foo;
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>Cheers,
style=3D'font-size:11.0pt;font-family:"Calibri","sans-serif" ;
color:#1F497D'>-Jan
style=3D'font-size:11.0pt;
font-family:"Calibri","sans-serif";color:#1F497D'>
>
0cm 4.0pt'>
0cm 0cm 0cm'>
style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"' >From:=
style=3D'font-size:10.0pt;font-family:"Tahoma","sans-serif"' >
activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of =
Deane.Rothenmaier@walgreens.com
Sent: December 4, 2007 11:56 AM
To: activeperl@listserv.ActiveState.com
Subject: another doggone newbie question about hash =
fiddling...
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> 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 =3D (
'01' =3D> 'abc/SID/def',
=
=
'02'
=3D> 'bcd/SID/fgh',
=
=
'03'
=3D> 'hjk/SID/opq' );
to =
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> $foo{'01'} =
=3D
'abc/12345/def';
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> $foo{'02'} =
=3D
'bcd/12345/fgh';
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> $foo{'03'} =
=3D
'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?
style=3D'font-size:10.0pt;font-family:"Arial","sans-serif"'> 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--
--===============0615181842==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0615181842==--
Re: another doggone newbie question about hash fiddling...
am 04.12.2007 22:14:11 von Andy_Bach
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@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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: another doggone newbie question about hash fiddling...
am 04.12.2007 22:20:42 von Andy_Bach
> 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
@foo = ('abc/SID/def', 'bcd/SID/fgh', 'hjk/SID/opq' );
foreach ( @foo ) {
s/SID/12345/;
}
or:
s/SID/12345/ for @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 ( @foo ) {
$foo =~ s/SID/12345/;
}
this doesn't mess w/ @foo.
a
Andy Bach
Systems Mangler
Internet: andy_bach@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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: another doggone newbie question about hash fiddling...
am 04.12.2007 22:41:48 von Jan Dubois
On Tue, 04 Dec 2007, Andy_Bach@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 ( @foo ) {
> $foo =~ s/SID/12345/;
> }
>
> this doesn't mess w/ @foo.
That is not correct; it will still modify @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 $_ ( @foo ) {
s/SID/12345/;
}
Cheers,
-Jan
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: another doggone newbie question about hash fiddling...
am 05.12.2007 03:20:00 von Justin Allegakoen
--------------8<---------------
Note that you can have a lexical $_ in 5.10:
foreach my $_ ( @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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: another doggone newbie question about hash fiddling...
am 05.12.2007 03:20:00 von Justin Allegakoen
--------------8<---------------
Note that you can have a lexical $_ in 5.10:
foreach my $_ ( @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@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: another doggone newbie question about hash fiddling...
am 05.12.2007 05:04:34 von Jan Dubois
On Tue, 04 Dec 2007, Justin Allegakoen wrote:
> --------------8<---------------
> Note that you can have a lexical $_ in 5.10:
>
> foreach my $_ ( @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. Moreover,
| declaring C restores the global C<$_> in the current scope.
Cheers,
-Jan
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs