usage of Split on pattern matching
--0-1741014498-1279541664=:75875
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hi all,
=A0
Iam beginner to perl & i have a small query on split.
using split i want print everything even the character used to split on str=
ing.
=A0
Here is my example :
=A0
my $string =3D "hi123now456itstimeforsplit789right"
my [at] array=A0 =3D split ( '/(\d+\s)/ ',$string);
## Trying to split using digitnumber followed by space
=A0
print " [at] array \n";
=A0
please explain if anything wrong with my syntax.
=A0
My assumption is in the given string as there is no space followed by digit=
s the output of array should be empty.
=A0
When I run the program ,it prints the whole string. so my assumption is wro=
ng.
=A0
please Could some one explain me on this?
=A0
=A0
Best Regards,
chandan.
=A0
=A0=0A=0A
--0-1741014498-1279541664=:75875--
Re: usage of Split on pattern matching
On Mon, Jul 19, 2010 at 08:14, Chandan Kumar <chandan_28582 [at] yahoo.com> wrot=
e:
snip
> my $string =3D "hi123now456itstimeforsplit789right"
> my [at] array=C2=A0 =3D split ( '/(\d+\s)/ ',$string);
snip
> When I run the program ,it prints the whole string. so my assumption is w=
rong.
>
> please Could some one explain me on this?
snip
The split function splits a string into fields based on a delimiter.
If the delimiter is not present then there is one field. What you are
describing is a match. Try this instead:
my [at] array =3D $string =3D~ /(.*?)([0-9]+)\s/g;
What this regex does is match the shortest string that is followed by
one or more digits followed by one whitespace character. It captures
the stuff before the digits and the digits. The g on the end causes
it to match as many times as it can, and the fact that the match is in
list context means that the captures will returned as a list. Note
the use of [0-9] instead of \d. The \d character class was expanded
in Perl 5.8 to include any Unicode digit character, so it is pretty
much useless now.
You may find [perldoc perlretut][0] and [perldoc perlre][1] useful.
[0]: http://perldoc.perl.org/perlretut.html
[1]: http://perldoc.perl.org/perlre.html
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: usage of Split on pattern matching
Chandan Kumar wrote:
> Hi all,
Hello,
> Iam beginner to perl& i have a small query on split.
> using split i want print everything even the character used to split on string.
>
> Here is my example :
>
> my $string = "hi123now456itstimeforsplit789right"
> my [at] array = split ( '/(\d+\s)/ ',$string);
> ## Trying to split using digitnumber followed by space
>
> print " [at] array \n";
>
> please explain if anything wrong with my syntax.
You are providing to split the first argument as the string '/(\d+\s)/ '
which is converted to the regular expression pattern /\/(\d+\s)\/ /. It
sounds like you want something like:
my [at] array = split /(\d+\s*)/, $string;
> My assumption is in the given string as there is no space followed by digits the output of array should be empty.
>
> When I run the program ,it prints the whole string. so my assumption is wrong.
>
> please Could some one explain me on this?
split removes whatever the pattern matches. If there is nothing to
remove then the whole string is returned as the first element of the list.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: usage of Split on pattern matching
--0-1994176380-1279558826=:60343
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Thanks guys, I could able to understand split fun.
--- On Mon, 19/7/10, John W. Krahn <jwkrahn [at] shaw.ca> wrote:
From: John W. Krahn <jwkrahn [at] shaw.ca>
Subject: Re: usage of Split on pattern matching
To: "Perl Beginners" <beginners [at] perl.org>
Date: Monday, 19 July, 2010, 4:29 PM
Chandan Kumar wrote:
> Hi all,
Hello,
> Iam beginner to perl&=A0 i have a small query on split.
> using split i want print everything even the character used to split on s=
tring.
>
> Here is my example :
>
> my $string =3D "hi123now456itstimeforsplit789right"
> my [at] array=A0 =3D split ( '/(\d+\s)/ ',$string);
> ## Trying to split using digitnumber followed by space
>
> print " [at] array \n";
>
> please explain if anything wrong with my syntax.
You are providing to split the first argument as the string '/(\d+\s)/ '
which is converted to the regular expression pattern /\/(\d+\s)\/ /.=A0 It=
sounds like you want something like:
my [at] array=A0 =3D split /(\d+\s*)/, $string;
> My assumption is in the given string as there is no space followed by dig=
its the output of array should be empty.
>
> When I run the program ,it prints the whole string. so my assumption is w=
rong.
>
> please Could some one explain me on this?
split removes whatever the pattern matches.=A0 If there is nothing to
remove then the whole string is returned as the first element of the list.
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0=A0-- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
=0A=0A
--0-1994176380-1279558826=:60343--
Can't use string 1 as HASH ref while strict refs in use
I passed a has reference to my sub and tried dereferencing it within the sub
and get an exception Can't use string ("1") as HASH ref while strict refs in
use.
my %mail_parameters = (
'Fname' => "$nickname_db_exist[1]",
'Lname' => "$nickname_db_exist[2]",
'E-mail' => "$reset_email",
'Subject' => "Your $site_name Password Reset",
);
# Send the requested information.
Mail_worker(\%mail_parameters);
Sub mail_worker {
$received_arg = [at] _;
%params = %$received_arg;# Can't use string 1 as HASH ref while strict
refs in use
.........
..............
}
Regards
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Can't use string 1 as HASH ref while strict refs in use
>>>>> "MC" == Mimi Cafe <mimicafe [at] googlemail.com> writes:
MC> $received_arg = [at] _;
read perldoc perldata and see what happens when you assign an array to a
scalar. you don't get what you think you are getting. context is VERY
important to learn in perl and this is a critical context situation that
is very common. i won't give you the answer since reading about it will
strengthten your learning about this.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Can't use string 1 as HASH ref while strict refs in use
On 20 July 2010 04:40, Mimi Cafe <mimicafe [at] googlemail.com> wrote:
> I passed a has reference to my sub and tried dereferencing it within the =
sub
> and get an exception Can't use string ("1") as HASH ref while strict refs=
in
> use.
>
>
> my %mail_parameters =3D (
> =A0 =A0 =A0 =A0'Fname' =3D> "$nickname_db_exist[1]",
> =A0 =A0 =A0 =A0'Lname' =3D> "$nickname_db_exist[2]",
> =A0 =A0 =A0 =A0'E-mail' =3D> "$reset_email",
> =A0 =A0 =A0 =A0'Subject' =3D> "Your $site_name Password Reset",
>
> =A0 =A0);
>
> =A0 =A0# Send the requested information.
> =A0 =A0Mail_worker(\%mail_parameters);
>
>
> Sub mail_worker {
>
> =A0 =A0$received_arg =3D [at] _;
> =A0 =A0%params =3D %$received_arg;# Can't use string 1 as HASH ref while =
strict
> refs in use
> =A0 =A0.........
> =A0 =A0..............
>
I hope Uri will forgive me but I will expand a little.
$received_arg =3D [at] _;
You want to shift the argument list but you are actually assigned the
argument array to a scalar value and that will render it's size (1).
You passed Mail_worker a reference to a hash so that hash is now a
single scalar value. Then in your subroutine (called mail_worker, you
dropped the case in your example) after assigning the length of the
argument list to $recieved_arg you attempt to deference it, causing
the error. You could have kept the hash as a reference
sub Mail_worker {
my $recieved_arg =3D shift;
....
print "User first name is $recieved_arg->{Fname}\n";
.....
}
You could have kept it as a hash but that's passing by value and
considered slightly poor practice as your passing more data that you
need. One more option would be to use a named parameter list
Mail_worker(Fname =3D> $nickname_db_exist[1]",
Lname =3D> "$nickname_db_exist[2]",
'E-mail' =3D> "$reset_email",
'Subject' =3D> "Your $site_name Password Reset",
);
sub Mail_worker {
my %args =3D [at] _
print "User first name is $recieved_arg{Fname}\n"
}
I hope that helps,
Dp.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: Can't use string 1 as HASH ref while strict refs in use
Oh, yes! I see. I went through the codes several times but did not pick that
mistake for some reason.
Perhaps I was working too late in the night :-)
This works: $received_arg = shift;
Thanks
=> -----Original Message-----
=> From: Uri Guttman [mailto:uri [at] StemSystems.com]
=> Sent: 20 July 2010 04:44
=> To: Mimi Cafe
=> Cc: 'Perl Beginners'
=> Subject: Re: Can't use string 1 as HASH ref while strict refs in use
=>
=> >>>>> "MC" == Mimi Cafe <mimicafe [at] googlemail.com> writes:
=>
=> MC> $received_arg = [at] _;
=>
=> read perldoc perldata and see what happens when you assign an array to
=> a
=> scalar. you don't get what you think you are getting. context is VERY
=> important to learn in perl and this is a critical context situation
=> that
=> is very common. i won't give you the answer since reading about it
=> will
=> strengthten your learning about this.
=>
=> uri
=>
=> --
=> Uri Guttman ------ uri [at] stemsystems.com --------
=> http://www.sysarch.com --
=> ----- Perl Code Review , Architecture, Development, Training, Support
=> ------
=> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --
=> -------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
MIME::Lite::TT::HTML - Default template
I am testing MIME::Lite::TT::HTML and it works as expected using my html
template. What I need to understand is why it does nit send email using my
text template or both template, since I did not explicitly specify which
template to use anywhere when calling new().
I specified 2 templates below following the MIME::Lite::TT::HTML synopsis,
but I can't see anywhere where it says use the html template. Is that the
default behaviour of this module to use an html template even if more than 1
is supplied?
Template => {
html => 'mail.html',
text => 'mail.txt',
},
I have not used Template::Toolkit before and the MIME::Lite::TT::HTML doc
relies on knowledge of Template::Toolkit.
Regards
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Can't use string 1 as HASH ref while strict refs in use
>>>>> "D" == Dermot <paikkos [at] gmail.com> writes:
D> I hope Uri will forgive me but I will expand a little.
partially forgive! :)
D> $received_arg = [at] _;
D> You want to shift the argument list but you are actually assigned the
D> argument array to a scalar value and that will render it's size (1).
D> You passed Mail_worker a reference to a hash so that hash is now a
D> single scalar value. Then in your subroutine (called mail_worker, you
D> dropped the case in your example) after assigning the length of the
D> argument list to $recieved_arg you attempt to deference it, causing
D> the error. You could have kept the hash as a reference
why do you say she wants to shift [at] _? that is the lesser style of
getting an arg from [at] _. the better way IMNSHO is assigning to a
list. shift is slower, doesn't work well for multiple args (one shift
per statement) and it modifies [at] _ unnecessarily. there are times when it
is useful but the primary method should be a list assignment.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: MIME::Lite::TT::HTML - Default template
2010/7/20 Mimi Cafe <mimicafe [at] googlemail.com>:
> I am testing MIME::Lite::TT::HTML and it works as expected using my html
> template. What I need to understand is why it does nit send email using my
> text template or both template, since I did not explicitly specify which
> template to use anywhere when calling new().
>
> I specified 2 templates below following the MIME::Lite::TT::HTML synopsis,
> but I can't see anywhere where it says use the html template. Is that the
> default behaviour of this module to use an html template even if more than 1
> is supplied?
>
When you use MIME::Lite you should have the chance to specify the
format for the message body, for example, is this a html message or a
plain text message. So I may think this behavior decide which template
your message should choose.
--
Jeff Pang
http://home.arcor.de/pangj/
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: MIME::Lite::TT::HTML - Default template
On Jul 20, 6:08=A0am, mimic... [at] googlemail.com (Mimi Cafe) wrote:
> I am testingMIME::Lite::TT::HTMLand it works as expected using myhtml
> template. What I need to understand is why it does nit send email using m=
y
> text template or both template, since I did not explicitly specify which
> template to use anywhere when calling new().
>
> I specified 2 templates below following theMIME::Lite::TT::HTMLsynopsis,
> but I can't see anywhere where it says use thehtmltemplate. Is that the
> default behaviour of this module to use anhtmltemplate even if more than =
1
> is supplied?
>
> Template =A0 =A0=3D> {
> =A0 =A0 =A0 =A0 =A0 =A0html=3D> 'mail.html',
> =A0 =A0 =A0 =A0 =A0 =A0 text =3D> 'mail.txt',
> =A0 =A0 =A0 =A0 },
>
> I have not used Template::Toolkit before and theMIME::Lite::TT::HTMLdoc
> relies on knowledge of Template::Toolkit.
>
> Regards
You are supposed to use *both* HTML and text parts for an HTML mail.
This is how HTML mail works. When you send an HTML to an email client
that supports HTML mail (almost everything does these days) your email
client will display HTML mail. However, if that email is opened in an
email client that does not support HTML mail (BlackBerry's and older
smartphones come to mind) the plaintext part will be displayed.
For testing the plaintext part of the email, you might look for an
option in your email client to use plaintext mail. If you have
Outlook you can move the email from your Inbox folder to the Junk Mail
(which will display plaintext by default).
Good luck!
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/