Ok,
I'm trying to count the number of characters in a string. Once the
count of character's reaches x I want to replace the rest of the
string with an web site link. So here's what I've come up with so
far, but I cannot figure out how to do the replace. Can you help me? I
cannot even get what I have to compile.
sub replaceLink {
my ($text) = @_;
my $textLength;
my $url;
my $maxLength;
my $count;
my $newText;
$maxLength = 305;
$textLength = length $_;
$url = "<a href='somwhere. html'>[View Article]</a>";
if ($textLength > 305) {
$count = 0;
while ($text =~ .\g){ # this is the error, . matching
# any character, g for greed
$count++;
if ($count == $maxLength){
$newText = $newText . $url;
}
else {
$newText = $newText . $2;
}
}
return $newText;
}
else {
return $text;
}
} 6 2519
$text = "qwertyuiopasdf ghjklzxcvbnm";
$maxLength = 5;
$weblink = 'http://www.google.com' ;
$text =~ s/(.{$maxLength}) .*?$/$1$weblink/;
print $text;
result:
qwerthttp://www.google.com ad******@comcas t.net wrote in message news:<dt******* *************** **********@4ax. com>... Ok,
I'm trying to count the number of characters in a string. Once the count of character's reaches x I want to replace the rest of the string with an web site link. So here's what I've come up with so far, but I cannot figure out how to do the replace. Can you help me? I cannot even get what I have to compile. sub replaceLink { my ($text) = @_; my $textLength; my $url; my $maxLength; my $count; my $newText;
$maxLength = 305; $textLength = length $_; $url = "<a href='somwhere. html'>[View Article]</a>";
if ($textLength > 305) { $count = 0; while ($text =~ .\g){ # this is the error, . matching # any character, g for greed $count++; if ($count == $maxLength){ $newText = $newText . $url; } else { $newText = $newText . $2; }
} return $newText; } else { return $text; }
}
"*
while ($text =~ .\g){ # this is the error, . matching
# any character, g for greed
*"
s/.// if you want to substitute any one charecter by nothing.
* and ? are the greedy operators, and *? and ?? for non-greedy.
you should realy check perl's regular expressions.
Yaroslav has solved your problem in a better way than this on you
code:
"* while ($text =~ .\g){ # this is the error, . matching # any character, g for greed $count++; if ($count == $maxLength){ $newText = $newText . $url; }
*" ad******@comcas t.net wrote: *Ok,
I'm trying to count the number of characters in a string. Once the count of character's reaches x I want to replace the rest of the string with an web site link. So here's what I've come up with so far, but I cannot figure out how to do the replace. Can you help me I cannot even get what I have to compile. sub replaceLink { my ($text) = @_; my $textLength; my $url; my $maxLength; my $count; my $newText;
$maxLength = 305; $textLength = length $_; $url = "<a href='somwhere. html'>[View Article]</a>";
if ($textLength > 305) { $count = 0; while ($text =~ .\g){ # this is the error, . matching # any character, g for greed $count++; if ($count == $maxLength){ $newText = $newText . $url; } else { $newText = $newText . $2; }
} return $newText; } else { return $text; }
}
-
nomerc
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------
Thank you both for your help. I've been having problems digesting
perlre so your help is deeply appreciated.
I have a couple of questions if you don't mind helping me
() are used to remember a match right? So why the . after ()?
(.{$maxLength}) .
Now, if i wanted to match the first whole word to the left of
$maxLength is this a good way to approach it (ensuring that a word is
not cut off) match a word boundry at the anchor?
$text =~ s/(.{$maxLength}) .*?$.\b/$1/$weblink/;
On 20 Apr 2004 07:24:04 -0700, ca**********@ya hoo.com (Yaroslav)
wrote: ad******@comcas t.net wrote: Thank you both for your help. I've been having problems digesting perlre
Then you may want to start with perlretut.
jue
[TOFU rearranged]
ad******@comcas t.net wrote in message news:<dt******* *************** **********@4ax. com>... Ok,
I'm trying to count the number of characters in a string. Once the count of character's reaches x I want to replace the rest of the string with an web site link. So here's what I've come up with so far, but I cannot figure out how to do the replace. Can you help me? I cannot even get what I have to compile.
In article <76************ **************@ posting.google. com>, Yaroslav
<ca**********@y ahoo.com> wrote:
$text = "qwertyuiopasdf ghjklzxcvbnm"; $maxLength = 5; $weblink = 'http://www.google.com' ;
$text =~ s/(.{$maxLength}) .*?$/$1$weblink/;
Why not just:
$text = substr($text,0. $maxLength) . $weblink;
?
print $text;
result:
qwerthttp://www.google.com
This newsgroup is defunct. Try comp.lang.perl. misc in the future. ad******@comcas t.net wrote: $text =~ s/(.{$maxLength}) .*?$/$1$weblink/; So why the . after ()?
You're looking at it wrong. There's not just a dot there, you need
to think of .*? as a single unit.
1) Read 'perldoc perlreftut' at least 3 times.
2) Post to comp.lang.perl. misc instead of comp.lang.perl next time.
-Joe This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steven Bethard |
last post by:
So I end up writing code like this a fair bit:
map = {}
for key, value in sequence:
map.setdefault(key, ).append(value)
This code basically constructs a one-to-many mapping -- each value that
a key occurs with is stored in the list for that key.
This code's fine, and seems pretty simple, but thanks to generator
|
by: Jeff Johnson [MVP: VB] |
last post by:
What is the point of lazy * and lazy ? ? "Nothing" will always succeed
first, right? If not, can someone give me an example of when either of these
might be used?
|
by: Montrose... |
last post by:
After working in c# for a year, the only conclusion I can come to is
that I wish I knew c.
All I need is Linux, the gnu c compiler and I can do anything.
Web services are just open sockets hooked up to interfaces.
The Gtk is more than enough gui.
|
by: Siemel Naran |
last post by:
What is a good idiom for handling a lazy object? I see 2 good
possibilities. Any more, any comments? Which way do people here use?
(1)
class Thing {
public:
Thing(double x, double y) : x(x), y(y), calculated(false) { }
double operator()() const {
|
by: Bill |
last post by:
Hello --
I'm a Java programmer who's slowly getting up to speed in Python. In
general I try to initialize the state of my objects as late as
possible, in the accessor. So if I have a member "_foo", my accessor
is something like:
public FooType getFoo() {
if (_foo == null) {
// initialize _foo here
| |
by: sturlamolden |
last post by:
Python allows the binding behaviour to be defined for descriptors,
using the __set__ and __get__ methods. I think it would be a major
advantage if this could be generalized to any object, by allowing the
assignment operator (=) to be overloaded.
One particular use for this would be to implement "lazy evaluation".
For example it would allow us to get rid of all the temporary arrays
produced by NumPy.
For example, consider the...
|
by: Boltar |
last post by:
Why does C do lazy evaluation for logical boolean operations but not
bitwise ones?
Ie: the following program prints "1 2" , not "1 1" under gcc
main()
{
int a = 1;
int b = 1;
0 && ++a;
|
by: fredd00 |
last post by:
Hi,
i'm trying to use lazy loading with Linq to sql and related objects
seems like you can only call the child object if the context is still
open, this is not real lazy loading.
here is my actual implementation
my class product has a categories collection (using a one-to-many
relation in .dbml)
|
by: Michael Bray |
last post by:
With the recent release of EF I've decided to dig into it a bit more than I
did before... the question I'm specifically interested in, but haven't
been able to find a resource to answer it is... when using Table-per-Type
with inherited entities, is it possible to lazy load the base classes?
For example, if I have an entity called "Person" and an entity called
"Employee" and (obviously) Employee inherits from Person, is it possible to...
|
by: Peng Yu |
last post by:
Hi,
I'm wondering if the following assignment is lazy copy or not?
Thanks,
Peng
std::vector<intv.
v.push_back(1);
v.push_back(2);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |