Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How can I split a string into fixed-length parts, rather than on delimiters?

by ruyler (Initiate)
on Jul 12, 2002 at 02:05 UTC ( [id://181193]=perlquestion: print w/replies, xml ) Need Help??

ruyler has asked for the wisdom of the Perl Monks concerning the following question: (strings)

I have a string (actually a file name) like "41084109.udp".
I want to get the first 4 chars into $var1 and the second 4 chars into $var2.

Originally posted as a Categorized Question.

  • Comment on How can I split a string into fixed-length parts, rather than on delimiters?
  • Download Code

Replies are listed 'Best First'.
Re: How can I split a string into fixed-length parts, rather than on delimiters?
by Kanji (Parson) on Jul 12, 2002 at 02:37 UTC

    unpack is arguably more appropriate than a regex:

    my $fname = '41084109.udp'; my( $v1, $v2 ) = unpack( 'a4 a4', $fname );
Re: How can I split a string into fixed-length parts, rather than on delimiters?
by jdporter (Paladin) on Apr 07, 2021 at 00:33 UTC

    You can combine two nifty (i.e. rarely used) tricks:

    sub fixed_width_records($$) { my( $size, $string ) = @_; open my $f, '<', \$string or die; # read from a string as if it were + a file local $/ = \$size; # "records" will be this many chars, rather than +to some end-of-line thing <$f> # parse! (i.e. read) and return the list of "records" } my( $first, $second ) = fixed_width_records( 4, "41084109.udp" ); print ">$first\n>$second\n";
Re: How can I split a string into fixed-length parts, rather than on delimiters?
by almut (Canon) on Nov 08, 2006 at 19:42 UTC

    Simply use m//g:

    my $fname = '41084109.udp'; @filename_parts = $fname =~ /(.{1,4})/g;
Re: How can I SPLIT a file name w/out delimiters ie: 41084109
by ackohno (Scribe) on Jul 12, 2002 at 03:31 UTC
    One way would be to do something like this:
    open(FILE,'41084109.udp'); $/=\4; $a = <FILE>; $b = <FILE>;
    The $/ variable determines how much to read in. In this case its set to 4, to read in 4 bytes (its default value is "\n").

    This might be more useful if you need more than two sets of four numbers, for something like:

    open(FILE,'41084109.udp'); $/=\4; while(<FILE>) { #count the occuarnce of the 4 digits $count{$_}++; }

    {QA Editors note: The original question's subject line was somewhat ambiguous as to whether the person asking was trying to split a file, or a filename. The followup text was still somewhat unclear, but the example shown by the original poster clarified that he was trying to split a filename. This response will aid in splitting the contents of a file, which is different but worthy of mention.}

    Originally posted as a Categorized Answer.

Re: How can I SPLIT a file name w/out delimiters ie: 41084109
by pemungkah (Priest) on Nov 08, 2006 at 01:37 UTC
    Yet another way, using the fact that if you put parens around the split() argument, it's kept in the output instead of discarded:
    @parts = split /(.{4})/, '41084109.udp';
    After this runs, print "@parts" will give you
    4108 4109 .udp
    Obviously, if the filename isn't exactly 8.4, you'll get the wrong answer, but it's an interesting use of split().
      Sounds like an overly complicated way of doing
      @parts = '41084109.udp' =~ /(.{4})/g;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found