Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Extracting full digits from a range

by smw6181 (Initiate)
on Aug 06, 2012 at 13:32 UTC ( #985709=note: print w/ replies, xml ) Need Help??


in reply to Re: Extracting full digits from a range
in thread Extracting full digits from a range

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


Comment on Re^2: Extracting full digits from a range
Select or Download Code
Re^3: Extracting full digits from a range
by choroba (Abbot) on Aug 06, 2012 at 13:43 UTC
    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.
Re^3: Extracting full digits from a range
by BrowserUk (Pope) on Aug 06, 2012 at 14:02 UTC
    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://985709]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-07-30 14:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls