Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Dealing with split

by dsb (Chaplain)
on Feb 22, 2006 at 22:19 UTC ( #532106=note: print w/replies, xml ) Need Help??

in reply to Dealing with split

You can still use split(), you just have to get a bit creative:
$str = 'c:\temp\source\test\test.c@@main\com\etc c:\temp\source\test\t +est1.c@@main\com\test c:\temp\source\test\test3\test2.c@@main\com\etc +\test'; print map {"$_\n" if s/c:\\temp(.*?\.c)/$1/} split(/\s+|\@+/,$str);
UPDATE: Thanks to Grandfather for the heads up on interpolation of my \'s. I did this correctly in my tests, but posted it wrong...thanks again.

SECOND UPDATE: I don't what happened between testing and posting that this code isn't working. It worked fine on my box.

THIRD AND (hopefully) FINAL UPDATE: Got it now. Thanks again to Grandfather.

This @ISA my( $cool ) %SIG

Replies are listed 'Best First'.
Re^2: Dealing with split
by GrandFather (Saint) on Feb 22, 2006 at 22:30 UTC

    You should really test your code before posting. All the \ dissapear from $str - use non-interpolating ' rather than " to fix that.

    There is a missing / in the split regex and an extra ) following it.

    Even after fixing those errors, when the code is run a "Use of uninitialized value in split" warning is issued and incorrect output is generated:

    \source\test\test.c@@main\com\etc c:\temp\source\test\test1.c@@main\co +m\test c:\temp\source\test\test3\test2.c@@main\com\etc\test

    DWIM is Perl's answer to Gödel
Re^2: Dealing with split
by perl_99_monk (Novice) on Feb 23, 2006 at 15:55 UTC
    dsb I am using your command and need a little more help. If I want to get all the files that has an extension not just .c how can i specify that? In the command I see that .c is specified. Can I specify mutliple patterns or use a wild card so that it gets all the files with extension.I have few .c files, .java and .xml and some directories in the $str. I just want to filter out the directories and get the files. ie in addition to
    'c:\temp\source\test\test.c@@main\com\etc c:\temp\source\test\test1.xm +l@@main\com\test c:\temp\source\test\test3\test2.c@@main\com\etc\test + c:temp\source c:\temp\source\java@@main\com\etc\test c:temp\source\ +java\\com\etc\test '
    I want the out put to be
    \test\test.c \source\test\test3\test2.c \test\test1.xml \source\java\
      You can tweak the regex to match all extensions or feed it an alternation that will find only certain extensions.
      # match any file extension print map {"$_\n" if s/c:\\temp(.*?\.(?:\w|\d)+?)/$1/} split(/\s+|\@+/ +,$str); # match only a few extensions print map {"$_\n" if s/c:\\temp(.*?\.(?:c|xml|java|etc))/$1/} split(/\ +s+|\@+/,$str);
      I think you'd be better of using runrig's snippet at Re: Dealing with split, though.

      This @ISA my( $cool ) %SIG

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://532106]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2021-12-04 16:45 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.