in reply to Transforming only parts of input

This way works...
my @values; while (<>) { my $line = $_; my @elements = split( /\s/, $line ); for ( @elements ) { if ( /(-I.:.*)/ ) { push (@values, $1) }; } } for ( @values ) { print "$_\n" };
     It uses part of Aristotle's idea with the reference to the first grouped part of the regular expression ($1).
     I couldn't get Aristotle's way to work, but I think it's because I always "use strict".

GM

Replies are listed 'Best First'.
Re: Re: Transforming only parts of input
by flounder99 (Friar) on Jun 26, 2002 at 18:54 UTC
    Aristotle's idea works fine for me.
    use strict; my @input = split /\n/, ' -IE:\david\nbssbts\btsc\bld -IE:\david\nbsscis\dcsapp\bld \ --cxx_include_directory C:\Green\scxx -IC:\Green\scxx \ -IC:\Green\68000\include -IC:\Green\ansi \ '; foreach (@input) { print "$1\n" while /\B(-I\S+)/g; } ___OUTPUT___ -IE:\david\nbssbts\btsc\bld -IE:\david\nbsscis\dcsapp\bld -IC:\Green\scxx -IC:\Green\68000\include -IC:\Green\ansi
    Of course this will not work if there are embedded spaces in the path names.

    Update --
    To handle embedded spaces use a zero-width lookahead.

    use strict; $_ = ' -IE:\david\nbssbts\btsc\bld -IE:\david\nbsscis\dcsapp\bld \ --cxx_include_directory C:\Green\scxx -IC:\Green\scxx \ -IC:\Green\68000\include -IC:\Green\ansi \ -ID:\Path with spaces\include \ '; s/\\\n//g; print "$1\n" while /(-I.+?(?= -I| --c|$))/g; __OUTPUT__ -IE:\david\nbssbts\btsc\bld -IE:\david\nbsscis\dcsapp\bld -IC:\Green\scxx -IC:\Green\68000\include -IC:\Green\ansi -ID:\Path with spaces\include

    --

    flounder

Re^2: Transforming only parts of input
by Aristotle (Chancellor) on Jun 26, 2002 at 19:07 UTC

    There's nothing in my code that violates strict, so I don't know why that would presumably be a problem. Try

    perl -Mstrict -wne'print "$1\n" while /\B(-I\S+)/g;' infile which is the same as
    #!/usr/bin/perl -w use strict; while(<>) { print "$1\n" while /\B(-I\S+)/g; }
    As a minor point, you preferrably want to split on \s+.

    Makeshifts last the longest.