Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > [Newbie] Stupid problem need simple answer (Array & RegExp)

Reply
Thread Tools

[Newbie] Stupid problem need simple answer (Array & RegExp)

 
 
Daedalus
Guest
Posts: n/a
 
      06-24-2004
> > > > > Your code (in all variants) has another problem. You are not
supposed
> > > > > to change an array while running a for-loop over it. (See

"foreach"
> > in
> > > > > perlsyn, I suppose.) That it appears to work in this instance

doesn't
> > > > > mean it will with other versions of Perl.
> > > >
> > > > AFAIK you are not supposed to change the array, but changing the

aliased
> > > > elements should be ok.
> > > >
> > > > so:
> > > >
> > > > push(@array, $_) for @array # not recommended
> > > >
> > > > s/A/B/g for @array # ok
> > > >
> > > > Right?
> > >
> > > Right.

> >
> > Well the problem is that s///g don't do what I want, since it change

every
> > thing at the same time.

>
> If you only want to change the first occurance in each string then
> remove the /g.
>
> If you want to append the changed version of the array to the original
> array then why not:
>
> s/A/B/g for my @array_copy = @array;
> push @array, @array_copy;
>
> > Anyway (for now) it couldn't turn into an infinite loop, since the

content
> > of the loop...

>
> I asume you are talking about the effect of pushing into @array at the
> same time with for().
>
> How do you know that?
>
> Bare in mind that the behaviour of for() when iterating over an array
> that grows or shrinks is _undefined_. Undefined means anything could
> happen, and also the thing that happens now may not be the thing that
> happens in the next version of Perl.
>
> If tomorrow the behaviour of pushing onto the array was to cause the
> loop to reset to the start of the array then it could very well create
> an infinite loop.
>
> > I know it's not the perfect solution (and I'll try to find a better way
> > while i'm on my learning process), so if someone have any idea... I'd be
> > glad to correct this misuse.

>
> See several better solutions already given in this thread.


Well sorry but at this time there no solutions in this thread that would
behave like I want.
Assuming that @string_list contain one string that is "to the bookstore"

foreach (@string_list){
my $string = $_;
push @string_list, $string if $string =~ s/(^to\b|^the\b)\s*//i;
}
this code would end with ("to the bookstore", "the bookstore", "bookstore")
and thats exactly the result i'm looking for.
While every other solutions proposed in the thread would end with ("to the
bookstore","the bookstore") or ("to the bookstore","bookstore") depending
wich of s///i or s///gi (or the map) is used.

Anyway if there's a way to do exactly the same thing with proper codes, it
would popup in my mind sooner or later, or in this newsgroup.

Thanks
DAE


 
Reply With Quote
 
 
 
 
Steven Kuo
Guest
Posts: n/a
 
      06-24-2004
On Thu, 24 Jun 2004, Daedalus wrote:

(snipped)
> >
> > I asume you are talking about the effect of pushing into @array at the
> > same time with for().
> >
> > How do you know that?
> >
> > Bare in mind that the behaviour of for() when iterating over an array
> > that grows or shrinks is _undefined_. Undefined means anything could
> > happen, and also the thing that happens now may not be the thing that
> > happens in the next version of Perl.
> >
> > If tomorrow the behaviour of pushing onto the array was to cause the
> > loop to reset to the start of the array then it could very well create
> > an infinite loop.
> >
> > > I know it's not the perfect solution (and I'll try to find a better way
> > > while i'm on my learning process), so if someone have any idea... I'd be
> > > glad to correct this misuse.

> >
> > See several better solutions already given in this thread.

>
> Well sorry but at this time there no solutions in this thread that would
> behave like I want.
> Assuming that @string_list contain one string that is "to the bookstore"
>
> foreach (@string_list){
> my $string = $_;
> push @string_list, $string if $string =~ s/(^to\b|^the\b)\s*//i;
> }
> this code would end with ("to the bookstore", "the bookstore", "bookstore")
> and thats exactly the result i'm looking for.
> While every other solutions proposed in the thread would end with ("to the
> bookstore","the bookstore") or ("to the bookstore","bookstore") depending
> wich of s///i or s///gi (or the map) is used.
>
> Anyway if there's a way to do exactly the same thing with proper codes, it
> would popup in my mind sooner or later, or in this newsgroup.
>
> Thanks
> DAE
>





Try something like the algorithms descibed in

perldoc -q 'expand tabs'
and
perldoc -q 'nesting':


#!/usr/local/bin/perl
use strict;
use warnings;
use Data:umper;

my @copy = my @strings = ( 'to the bookstore' );

for (@strings) {
push @copy, $_ while (s/^(to|the)\b\s*//i);
}

print Dumper \@copy;


__END__
$VAR1 = [
'to the bookstore',
'the bookstore',
'bookstore'
];

--
Hope this helps,
Steven


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-24-2004
Daedalus <(E-Mail Removed)> wrote in comp.lang.perl.misc:
[more attributions lost]

[big snip]

> > If tomorrow the behaviour of pushing onto the array was to cause the
> > loop to reset to the start of the array then it could very well create
> > an infinite loop.
> >
> > > I know it's not the perfect solution (and I'll try to find a better way
> > > while i'm on my learning process), so if someone have any idea... I'd be
> > > glad to correct this misuse.

> >
> > See several better solutions already given in this thread.

>
> Well sorry but at this time there no solutions in this thread that would
> behave like I want.
> Assuming that @string_list contain one string that is "to the bookstore"
>
> foreach (@string_list){
> my $string = $_;
> push @string_list, $string if $string =~ s/(^to\b|^the\b)\s*//i;
> }
> this code would end with ("to the bookstore", "the bookstore", "bookstore")


No, it wouldn't. You'd need "while" instead of "if" inside the loop.

> and thats exactly the result i'm looking for.


Well, then do it in a way that doesn't violate the rules. The most
obvious way (already suggested, I think), is to use a copy of the list
to loop over:

foreach ( my @copy = @string_list ) {
push @string_list, $_ while s/(^to\b|^the\b)\s*//i;
}

You don't need to copy $_ inside the loop because you already copied
all the values.

> While every other solutions proposed in the thread would end with ("to the
> bookstore","the bookstore") or ("to the bookstore","bookstore") depending
> wich of s///i or s///gi (or the map) is used.


That would be mostly because your original example (about "a" and "the")
did in no way suggest this requirement.

> Anyway if there's a way to do exactly the same thing with proper codes, it
> would popup in my mind sooner or later, or in this newsgroup.


See above.

Anno
 
Reply With Quote
 
Daedalus
Guest
Posts: n/a
 
      06-24-2004
> Try something like the algorithms descibed in
>
> perldoc -q 'expand tabs'
> and
> perldoc -q 'nesting':
>
>
> #!/usr/local/bin/perl
> use strict;
> use warnings;
> use Data:umper;
>
> my @copy = my @strings = ( 'to the bookstore' );
>
> for (@strings) {
> push @copy, $_ while (s/^(to|the)\b\s*//i);
> }


That's exactly what I want ... push into a copy and test the original
"while" instead of "if". (it's a shame... so simple).

Thanks


 
Reply With Quote
 
Daedalus
Guest
Posts: n/a
 
      06-24-2004
>> While every other solutions proposed in the thread would end with ("to
the
> >bookstore","the bookstore") or ("to the bookstore","bookstore") depending
> >wich of s///i or s///gi (or the map) is used.

>
> That would be mostly because your original example (about "a" and "the")
> did in no way suggest this requirement.


Yeah that's totaly true. Actually when I posted the original exemple, I
didn't know that my foreach-loop violates the rules. My question was about
"When I write: if ($string =~ s/(^the\b)|(^a\b)|//i)..., do I'm actually
replacing the string within @string_list " And the answer was yes, in
"foreach my $string (@string_list)" $string become an aliase of the current
element of @string_list.
Then you make the point that my loop was illegal, so I had to change my
exemple to explain why I did use the loop that way, and see if someone could
point me to a legal way to obtain the same behavior... and here we are.


Finally, as I said in the topic, it was a stupid and simple thing: using
while with a copy (sorry I didn't thought of "while"). But in fact, why
should I thought of it ? Since I didn't realise the loop I did was illegal.


Don't worry, I'll just be a newb for a while

Thanks to all (and sorry if I seem rude sometimes, english is not my primary
language)

DAE


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stupid question...waiting for a stupid answer Brandon McCombs Java 4 08-28-2006 06:57 PM
Wrong answer equals to a blank answer or not? Zadkin Microsoft Certification 8 06-27-2006 01:51 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM



Advertisments