Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Perl memset equivalent?

by jkoval (Initiate)
on Jun 26, 2012 at 18:36 UTC ( #978482=perlquestion: print w/replies, xml ) Need Help??
jkoval has asked for the wisdom of the Perl Monks concerning the following question:

Hi - I'm wondering if there's a perl functional equivalent of this specific C implementation of memset? In this case, STN_NAME_LEN is equal to "5". Thank you.

memset(stn_names[recordCount], 0, STN_NAME_LEN);

Replies are listed 'Best First'.
Re: Perl memset equivalent?
by moritz (Cardinal) on Jun 26, 2012 at 18:37 UTC

    You don't have direct memory access in Perl, so talking about an equivalent is a bit misleading.

    If you want to set a range of array elements to zero, you can do that as

    @array[0..4] = (0) x 5;

    If you want to create a string of zero-bytes, you can write

    my $s = "\0" x 5;

    (Update: fixed off-by-one error noticed by martin++).

Re: Perl memset equivalent?
by BrowserUk (Pope) on Jun 26, 2012 at 18:37 UTC


    use constant STN_NAME_LEN => 5; $stn_names[ $recordCount ] = chr(0) x STN_NAME_LEN;

    But there is bound to be more behind this question than you are telling us.

    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?

Re: Perl memset equivalent?
by sauoq (Abbot) on Jun 26, 2012 at 18:43 UTC

    It's hard to guess what you are trying to achieve exactly... Do you want:

    @stn_names = (0) x STN_NAME_LEN;
    which gives you an array filled with five zeros? Or do you want something more like:
    my $s = pack "c5", (0)x5;
    which will give you a string packed with 5 nulls?

    "My two cents aren't worth a dime.";
Re: Perl memset equivalent?
by Anonymous Monk on Jun 27, 2012 at 00:18 UTC
    C has no "smart" or particularly "clever" data structures; nothing like a hash or what Perl refers-to as an array. When you look at an existing C program, study what it does ... not how it does it. The choices that a programmer must make when using one language are partly dependent on the task and mostly dependent on the language. The only things that should carry-forward in a reimplementation project are the tasks to be performed, not how it is being done by the program that is being replaced.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://978482]
Approved by ig
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-12-11 23:02 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (317 votes). Check out past polls.