Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Regular expression assistance

by kennethk (Abbot)
on Dec 07, 2012 at 15:48 UTC ( #1007765=note: print w/replies, xml ) Need Help??


in reply to Regular expression assistance

For me, the code print "yes\n" if $cdp =~ /0x01/; prints yes which means that your regular expression is valid. Can you show us a little more surrounding code? What do you expect to find in your array when you are done? Mine contains a 1, because that's what m// returns on a successful match.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Regular expression assistance
by agentorange (Sexton) on Dec 07, 2012 at 15:56 UTC
    Thanks.

    Sorry didn't explain it fully but whilst I can pattern match ok I cannot see how to pattern match and pull out the 8th field in the line of which <space> is the separator char.
      Okay, much better. Assuming you want to do this in the midst of a script and not just piping one liners about, I would use more operations rather than be super sparse on characters. First, since you have a string that is internally-delimited by newlines, you can use . to match any character that is not a new line. This means you can actually grab your line using $cdp =~  m/(.*0x01.*)/, storing the line in $1. You could then split that on whitespace and grab the 8th field with (split /\s+/, $1)[7]. So collecting all your 8th field matches:
      my @switch; while ($cdp =~ m/(.*0x01.*)/g) { push @switch, (split /\s+/, $1)[7]; }
      If this is not your intent and the way forward is unclear, post explicit desired output so the goal is clear.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Ah perfect thank you.

        I hadn't appreciated I could reference the 8th element with 7 around split()

        Many thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007765]
help
Chatterbox?
[ambrus]: erix: that one actually sucks. these days people should get rid of the old notion that TeX is the only thing you can use for decent mathematics writing, because MS Office and LibreOffice have reached the
[ambrus]: level where people can more easily write as good mathematical papers in them as the people who write bad LaTeX papers usually write.
[ambrus]: Yes, for like the first twenty of its years, TeX was basically the only system that allowed you to write decent maths papers, and C++ and PHP were programming languages that sucked, etc. But times change and people have to accept that.
Discipulus bad people + good tool < normal people + decent tool
[Discipulus]: php does not suck anymore?
[ambrus]: Discipulus: I'm not sure, but it certainly doesn't suck as much as it's used to. it's like C++, it sucks because people still recursively learn from twenty year old PHP examples,
[ambrus]: and they try to use the obsolete features that PHP has to support only for compatibility with old scripts. C++ and PHP both have the problem that people can't forget the past, because when they google "PHP" plus the problme they want to solve, they find b
[ambrus]: ad code examples.
[ambrus]: I'm not trying to recommend PHP, but I think it has way too bad a name because of its past.
[ambrus]: This is different from MS Word, which was already a good editor in the pre-unicode days (in word for windows versions 2 and 6, which ran on windows 3 but also on windows 95), only it wasn't trying to solve the task of writing maths papers back then.

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-09-26 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?