Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

seperating a string with substr...

by abachus (Monk)
on Jun 29, 2006 at 23:32 UTC ( #558471=perlquestion: print w/replies, xml ) Need Help??

abachus has asked for the wisdom of the Perl Monks concerning the following question:

Good day/night all,

Another simple question of many i seem to bring, i was wondering if you might be able to help me with this thing :

while($data){ $output=substr(0,8,''); print $output; }

The above code appears to work for me, and i even think i understand it, well i'd appreciate if you could point out if i'm wrong, or better still advise a better way of seperating the data.

1. While $data is true, that is while it contains bytes, except if there remains one byte that happens to be zero(0), in which case its false. To get around that could be :

while(length($data) > 0){;}

2. $output is filled with the first 8 bytes of data, with the substr() function and those bytes within $data are replaced with (no data), thus $data is reduced in size by indeed 8 bytes. Prints $output illustration purposes obviously.

thanks again,

Isaac.

Replies are listed 'Best First'.
Re: seperating a string with substr...
by davidrw (Prior) on Jun 29, 2006 at 23:44 UTC
    I think you meant substr($data,0,8,'') .. otherwise $data doesn't get modified and it'll be an infinite loop :)

    Note that re: your #1, $data is also logically false if it is an empty string '' or undef (see True or False? A Quick Reference Guide for a good reference)

    As for an alternative, you could also use a regex (there's probably a split solution as well--this is close: my @pieces = split /(.{1,3})/, $x):
    my @pieces = $data =~ /(.{1,8})/g;
      A little cleaner than m//g or split is to use unpack for this purpose:
      my @pieces = unpack "(A8)*", $data;
      Someone correct me if I'm wrong, but I seem to remember that this unpack syntax (paren-groupings and star) may not have been around for all of Perl 5, so it's worth double-checking with your version of perl.

      blokhead

      If $data may contain newlines, you'd want instead:
      my @pieces = $data =~ /.{1,8}/gs;
      (Also note that your () were unnecessary, since list context //g will return the entire match if there are no submatches.)

      ooops :) fwiw i have an open window with the code and it has substr($data,0,8,'') in it! I was trying to word the english properly and overlooked the most important bit, the code...previewed like about 3 times aswell, 'shrug' oh well.

      As for if $data contains no bytes, or undef or a zero its false isn't it ? nothing else ?

      The regex bit looks interesting though, i think i'll go have a play with that, thanks !

      Isaac.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://558471]
Approved by davidrw
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2022-08-07 19:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?