Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sub question

0 views
Skip to first unread message

Josh Ghiloni

unread,
Dec 21, 2001, 10:06:47 AM12/21/01
to
Baby's first post to comp.lang.perl.misc :)

I have what is probably a fairly simple question that is baffling me. I
have some code that is basically a very simple search and replace routine.
I want to place it in a subroutine, do the specified function, and return
a value. The code itself works. However, when i try to place it in a sub,
it doesn't return anything, nor does it give errors. i've attached a
relevant code snippet so you can see what i'm talking about. BTW, this is
perl 5.6.0 on OpenBSD 2.9.

Thanks a lot guys (and gals),
Josh

-- BEGIN WORKING CODE --
$source = "blah";
%map = ('a' => 'foo',
'b' => 'bar'); #etc, etc, etc.

@chars = split //, $source;
$new = "";

foreach (@chars) {
$new .= (defined ($map{$_}) ? $map{$_} : $_);
}

# do more specialized replaces here...
$new =~ s/quxx/larry/g;
$new =~ s/(\w+)\./$1!/g;
# etc. etc. etc.
-------------------------------

I'd like to have:
------------------
%map = ('a' => 'foo',
'b' => 'bar'); #etc, etc, etc.

sub dostuff {
$source = shift;
@chars = split //, $source;
$new = "";

foreach (@chars) {
$new .= (defined ($map{$_}) ? $map{$_} : $_);
}

# do more specialized replaces here...
$new =~ s/quxx/larry/g;
$new =~ s/(\w+)\./$1!/g;
# etc. etc. etc.

return $new;
}

$newstring = dostuff "Hello, World.";
print $newstring;
---------------------------------

------------------------------------------------------------------
Josh Ghiloni - jo...@joshghiloni.net - http://www.joshghiloni.net/

Webmaster, WRUW-FM <http://www.wruw.org/>
Postmaster, The Geek Empire <http://www.thegeekempire.net/>
------------------------------------------------------------------


Bob Walton

unread,
Dec 21, 2001, 5:47:35 PM12/21/01
to
Josh Ghiloni wrote:
...

me. I
> have some code that is basically a very simple search and replace routine.
> I want to place it in a subroutine, do the specified function, and return
> a value. The code itself works. However, when i try to place it in a sub,
> it doesn't return anything, nor does it give errors. i've attached a
> relevant code snippet so you can see what i'm talking about. BTW, this is
> perl 5.6.0 on OpenBSD 2.9.
...
> Josh
...

> %map = ('a' => 'foo',
> 'b' => 'bar'); #etc, etc, etc.
>
> sub dostuff {
> $source = shift;
> @chars = split //, $source;
> $new = "";
>
> foreach (@chars) {
> $new .= (defined ($map{$_}) ? $map{$_} : $_);
> }
>
> # do more specialized replaces here...
> $new =~ s/quxx/larry/g;
> $new =~ s/(\w+)\./$1!/g;
> # etc. etc. etc.
>
> return $new;
> }
>
> $newstring = dostuff "Hello, World.";
> print $newstring;
......
What exactly is it that you think doesn't work about your code? When I
run it verbatim, it prints:

Hello, World!

just like it should.

The only real criticism is that you should make the variables used in
the sub lexical so that when you sub is run it does not alter variables
in the calling workspace. That means your should put "my" in front of
them the first time they are used in the sub. One could clean up the
code a lot and make it run much faster, but that wasn't your issue.
--
Bob Walton

Tad McClellan

unread,
Dec 21, 2001, 6:12:09 PM12/21/01
to
Josh Ghiloni <jo...@joshghiloni.net> wrote:

>Baby's first post to comp.lang.perl.misc :)


Welcome!

There are some Posting Guidelings that may be helpful to you:

http://mail.augustmail.com/~tadmc/clpmisc.shtml


You should start all your Perl programs like this:

#!/usr/bin/perl -w
use strict;

That is, enable warnings and strictures.


>I have what is probably a fairly simple question that is baffling me.


It is baffling me too.


>The code itself works. However, when i try to place it in a sub,
>it doesn't return anything, nor does it give errors.


Have you put a print() statement or something inside of the
subroutine body to see if it is actually getting called?

Are you running this from the command line?


>I'd like to have:


Your subroutine works fine for me.

But your test string doesn't contain any of %map's keys :-)

After fixing the useless test into a more useful one, your
subroutine still works fine for me.

>%map = ('a' => 'foo',
> 'b' => 'bar'); #etc, etc, etc.
>
>sub dostuff {
> $source = shift;
> @chars = split //, $source;


You do not need either of those temporary variables.


> $new = "";
>
> foreach (@chars) {


foreach ( split //, shift ) {


> $new .= (defined ($map{$_}) ? $map{$_} : $_);
> }
>
> # do more specialized replaces here...
> $new =~ s/quxx/larry/g;
> $new =~ s/(\w+)\./$1!/g;
> # etc. etc. etc.
>
> return $new;
>}
>
>$newstring = dostuff "Hello, World.";
>print $newstring;


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

Jon Ericson

unread,
Dec 21, 2001, 9:56:29 AM12/21/01
to
Josh Ghiloni <jo...@joshghiloni.net> writes:

> I'd like to have:
> ------------------
> %map = ('a' => 'foo',
> 'b' => 'bar'); #etc, etc, etc.
>
> sub dostuff {
> $source = shift;
> @chars = split //, $source;
> $new = "";
>
> foreach (@chars) {
> $new .= (defined ($map{$_}) ? $map{$_} : $_);
> }
>
> # do more specialized replaces here...
> $new =~ s/quxx/larry/g;
> $new =~ s/(\w+)\./$1!/g;
> # etc. etc. etc.
>
> return $new;
> }
>
> $newstring = dostuff "Hello, World.";
> print $newstring;

Hmmm, works ok for me:

$ perl test
Hello, World!

You will want to start using strictures and warnings:

#!/usr/bin/perl -w
use strict;

as you start to write larger more complicated code.

Jon
--
Two are better than one, because they have a good return for their
work: If one falls down, his friend can help him up... Though one
may be overpowered, two can defend themselves. A cord of three
strands is not quickly broken. -- Ecclesiastes 4:9,12 (NIV)

Developer

unread,
Dec 21, 2001, 6:59:35 PM12/21/01
to
This code works good
but Hello World doesn't contain 'a' to replace it with foo, and doesn't contain 'b' to replace it with bar :)

---------------------------------

Josh Ghiloni

unread,
Dec 21, 2001, 5:32:18 PM12/21/01
to
:What exactly is it that you think doesn't work about your code? When I

:run it verbatim, it prints:
:
: Hello, World!
:
:just like it should.
When I ran it it printed nothing.

:
:The only real criticism is that you should make the variables used in


:the sub lexical so that when you sub is run it does not alter variables
:in the calling workspace. That means your should put "my" in front of
:them the first time they are used in the sub.

Will do that.

Josh Ghiloni

unread,
Dec 21, 2001, 8:34:19 PM12/21/01
to
I know it is rather gauche to follow-up ones own post, but i got it
working. Don't know what i did differently, but it works, so frankly, i
don't give a hoot :) Thanks for the help to those who did. I did add use
strict; and like it more :)

------------------------------------------------------------------

Webmaster, WRUW-FM <http://www.wruw.org/>

Jay Tilton

unread,
Dec 22, 2001, 1:27:30 AM12/22/01
to
Josh Ghiloni <jo...@joshghiloni.net> wrote:

>However, when i try to place it in a sub,
>it doesn't return anything, nor does it give errors. i've attached a
>relevant code snippet so you can see what i'm talking about. BTW, this is
>perl 5.6.0 on OpenBSD 2.9.

[snip]

Just a WAG. Maybe printed output is being generated, but is
being overwritten on screen by the next command prompt. Try
tacking "\n" onto the print, or redirect output to a file.

0 new messages