split and substitute, substitute, substitute 
Author Message
 split and substitute, substitute, substitute

Three questions in one post - tell me if I'm cheating...

i) say I have a line:

preamble more_junk at_last another_variable and_another not_this
etc...

I'd like to be able to assign several scalars from this all at once,
so I know I can do this:

$unwanted = $_;

$unwanted =~ s/^\S+\s+\S+\s(\S+)\s(\S+) .../\1,\2,\3.../;
($var1, $var2, $var3, ...) = split ',',$unwanted;

but what I really want is a one liner... I've tried loads of
combinations (danged Perl's just too flexible) but still can't find
the right one... (but only if you can use it)

please write answer in here________

ii) say I have a line:

preamble more_junk at_last OPTIONAL CONDITIONAL_ON_OPTIONAL
another_variable

I'm chaining together series of the two-liners in i)

anyway of doing other than

$unwanted = $_;

$unwanted =~ s/^\S+\s+\S+\s+(\S+)\s+(.*)/\1,\2/;
($var1, $rest) = split ',',$unwanted;

if ($rest =~ /^Optional/) {
  ...
  etc ad nauseum

Quote:
}

And last but not least...

iii) how do I substitute only LEADING spaces/whitespace for an
equivalent number of bangs??

TIA etc. etc.
Gregski



Fri, 20 Aug 2004 12:45:04 GMT  
 split and substitute, substitute, substitute

Quote:
> Three questions in one post - tell me if I'm cheating...

You're cheating.

Quote:
> i) say I have a line:

> preamble more_junk at_last another_variable and_another not_this
> etc...

> I'd like to be able to assign several scalars from this all at once,
> so I know I can do this:

> $unwanted = $_;

Totally unnecessary.

Quote:
> $unwanted =~ s/^\S+\s+\S+\s(\S+)\s(\S+) .../\1,\2,\3.../;
> ($var1, $var2, $var3, ...) = split ',',$unwanted;

> but what I really want is a one liner... I've tried loads of
> combinations (danged Perl's just too flexible) but still can't find
> the right one... (but only if you can use it)

> please write answer in here________

If you're trying to be funny, try harder.

(undef, undef, $var1, $var2, $var3) = m/(\S+)/g;

Quote:
> ii) say I have a line:

> preamble more_junk at_last OPTIONAL CONDITIONAL_ON_OPTIONAL
> another_variable

Do it like above and then test the vars which are optional. They
should be undefined if they're not present in the input. Or use the ?
operator in the regex:

     perldoc perlre

Quote:
> And last but not least...

> iii) how do I substitute only LEADING spaces/whitespace for an
> equivalent number of bangs??

s/^(\s+)/'#' x length $1/e;

Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'



Fri, 20 Aug 2004 12:57:30 GMT  
 split and substitute, substitute, substitute

Quote:
> > please write answer in here________

> If you're trying to be funny, try harder.

or trying to deflect any criticism on my ignorance of perl :-}

Quote:
> (undef, undef, $var1, $var2, $var3) = m/(\S+)/g;

dang! you half find a way of doing something so you don't try from a
different angle!

Quote:
> > ii) say I have a line:

> > preamble more_junk at_last OPTIONAL CONDITIONAL_ON_OPTIONAL
> > another_variable

> Do it like above and then test the vars which are optional. They
> should be undefined if they're not present in the input. Or use the ?
> operator in the regex:

but the optionals are in the middle of the line - if they're not
there, then I end up having the last 2 (or 3) parameters undefined.
e.g.

a b c d [e f [g]] h i j

into a hash %z

I would have to check for undef of $z{'h'}, $z{'i'}, $z{'j'} because
$z{'e'}, $z{'f'}... would have their respective assignments, then
re-assign...

Quote:
>      perldoc perlre
> > And last but not least...

> > iii) how do I substitute only LEADING spaces/whitespace for an
> > equivalent number of bangs??

> s/^(\s+)/'#' x length $1/e;

it's so obvious in retrospect!!


Fri, 20 Aug 2004 16:11:10 GMT  
 split and substitute, substitute, substitute
[posted & mailed]

On Mar 4, Gregski said:

Quote:
>preamble more_junk at_last another_variable and_another not_this
>etc...

>$unwanted = $_;
>$unwanted =~ s/^\S+\s+\S+\s(\S+)\s(\S+) .../\1,\2,\3.../;

You should use $1 and not \1 on the right-hand side of a s///.

Quote:
>($var1, $var2, $var3, ...) = split ',',$unwanted;

Use undef to discard unwanted fields:

  # assuming the string is in $_
  # split() is split(' ', $_)
  (undef, undef, $this, $that, $those) = split;

Quote:
>ii) say I have a line:

>preamble more_junk at_last OPTIONAL CONDITIONAL_ON_OPTIONAL
>another_variable

>$unwanted = $_;

>$unwanted =~ s/^\S+\s+\S+\s+(\S+)\s+(.*)/\1,\2/;
>($var1, $rest) = split ',',$unwanted;

>if ($rest =~ /^Optional/) {
>  ...
>  etc ad nauseum
>}

Well, try applying what I showed you above.

Quote:
>iii) how do I substitute only LEADING spaces/whitespace for an
>equivalent number of bangs??

You could use

  s/\G\s/!/g;

or

  s/^(\s+)/"!" x length($1)/e;

--

RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



Fri, 20 Aug 2004 22:40:59 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. split and substitute ?

2. substituting english alphabets with english and accented characters

3. substitute command generates weird characters

4. howto substitute hash

5. Substituting many strings in one pass over the input

6. recursive substitute expressions..

7. Substituting many sparsely distrbuted strings in many files

8. substitute of space problem - newbie

9. substituting for matches not inside quotes?

10. Search and substitute...

11. substituting strings in source trees?

12. substitute first match?

 

 
Powered by phpBB® Forum Software