Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Extracting full digits from a range

by BrowserUk (Pope)
on Aug 04, 2012 at 00:39 UTC ( #985369=note: print w/ replies, xml ) Need Help??


in reply to Extracting full digits from a range

'293F:2945' =~ /([0-9a-f]+):([0-9a-f]+)$/i and @range = map sprintf( '%x',$_ ), eval "0x$1 .. 0x$2";; print @range;; 293f 2940 2941 2942 2943 2944 2945

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re: Extracting full digits from a range
Download Code
Re^2: Extracting full digits from a range
by smw6181 (Initiate) on Aug 06, 2012 at 13:32 UTC

    Thanks for the help everyone. This one looks like it will fit best into my script, however I'm having one more issue, and it's no doubt something really ridiculous I'm doing wrong.

    I have the following code snippet:

    #!c:/perl/bin/perl use strict; open (HYPERS, "< hypers.txt"); my @hypers = <HYPERS>; chomp @hypers; print "Hypers array: @hypers\n"; foreach my $line(@hypers){ $line =~ /([0-9a-f]+):([0-9a-f]+)$/i and my @range = map sprintf( '%x,',$_ ), eval "0x$1 .. 0x$2";; print @range;; } Output: Hypers array: 293F:2946

    So I know the array is being read, but when trying to shove it into that regex it's coming out as '', almost like it's not there.

    The HYPERS file looks like this:

    293F:2946
      Works for me. Is there any whitespace in the input file (a space at the end of the line)?
      You migh also suffer from buffering: print a newline after printing the range.
      it's no doubt something really ridiculous I'm doing wrong.

      Dunno! Your code and your hypers file produce the apparently desired result on my machine:

      C:\test>type junk12.pl #!c:/perl/bin/perl use strict; open (HYPERS, "< hypers.txt"); my @hypers = <HYPERS>; chomp @hypers; print "Hypers array: @hypers\n"; foreach my $line(@hypers){ $line =~ /([0-9a-f]+):([0-9a-f]+)$/i and my @range = map sprintf( '%x,',$_ ), eval "0x$1 .. 0x$2";; print @range;; } C:\test>type hypers.txt 293F:2946 C:\test>junk12 Hypers array: 293F:2946 293f,2940,2941,2942,2943,2944,2945,2946, C:\test>

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        Well it's good to hear my coding is right.....and I tried removing all whitespace, still produces a blank output. Same with putting a \n after @range. I'm completely confused now though. printing out $line also produces '', so it seems that something's going wrong with parsing the original infile. Guess that's where I need to look. Thanks for confirming my basic code is right folks.

        Found it. It's definitely whitespace at the end of the line in the text file. Not sure why $line =~ s/\G //g; didn't work, but I'll figure it out. Thanks for pointing me in the right direction folks!

        Last update: $line =~ s/^\s+|\s+$//g; is the regex I was looking for. That does the trick. Thank you all again, I really appreciate the help!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-07-31 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls