小编Dan*_*her的帖子

Perl正则表达式用正则表达式的子字符串替换字符串

我有一个关于Perl的问题.

假设我的文件中有以下行:

DoMatchLong ( "ThisIsMyVariable", lThisIsMyVariable);
Run Code Online (Sandbox Code Playgroud)

我想用cp_const_ThisIsMyVariable替换"ThisIsMyVariable"

所以我的目标是:

DoMatchLong (  cp_const_ThisIsMyVariable, lThisIsMyVariable);


$count = s/(?<=")\w+(?=")/const_cmd_cp_$&/gx;
Run Code Online (Sandbox Code Playgroud)

导致DoMatchLong ( "cp_const_ThisIsMyVariable", lThisIsMyVariable);

那么正确的解决方案是什么?

regex perl substring

1
推荐指数
1
解决办法
972
查看次数

如何创建自己的扩展用户表单?

我有扩展用户的django模型:

class Student(models.Model):
   user = models.OneToOneField(User, unique=True)
   #other field in that profile
   #other field in that profile
   #other field in that profile
Run Code Online (Sandbox Code Playgroud)

在settings.py中添加了:

AUTH_PROFILE_MODULE = 'myapp.Student'
Run Code Online (Sandbox Code Playgroud)

现在我希望在我的网站上有一些表格来创建该学生用户.最简单的方法是什么?我不知道我应该在forms.py中创建ModelForm,forms.Form还是其他东西.此外,我不知道如何在views.py文件中验证此表单.我只想添加这个学生额外字段的新用户.我还在尝试一些方法,但没有任何作用!请帮忙!

我正在使用Django1.2.5

forms django

1
推荐指数
1
解决办法
4731
查看次数

C/C++中的快速字符串标记化

我正在开发一个C/C++应用程序(在Visual Studio 2010中),我需要对逗号分隔的字符串进行标记,我希望它尽可能快.目前我正在使用strtok_s.我跑了一些测试strtok_s对比sscanf,它似乎就像strtok_s是快(除非我写了一个可怕的实现:)),但我想知道如果任何人都可以提出一个更快的替代方案.

c c++ string visual-studio-2010 tokenize

1
推荐指数
2
解决办法
7036
查看次数

如何将结构指针数组初始化为零

我正在为通过malloc构造的指针数组分配内存,并希望用零初始化它,如下所述.假设结构包含int和char [](字符串)类型的成员?那我怎么能把这个结构归零呢.

代码:假设我想分配100

struct A **a = NULL;
a = (struct **)malloc(sizeof( (*a) * 100);
for(i=1; i < 100; i++)
     a[i] = (struct A*)malloc(sizeof(a));
Run Code Online (Sandbox Code Playgroud)

还请解释一下为什么有必要用零初始化.

平台:Linux,编程语言:C

我知道我们可以使用memset或bzero.我试过它它崩溃了,可能是我正确使用它所以请告诉我正确的方法.

c linux pointers structure

1
推荐指数
1
解决办法
3048
查看次数

我有一个游戏循环,由于某种原因无法运作

我在制作这个循环工作时遇到了麻烦,任何帮助/建议都会非常感激.

这是循环(它不是完整的程序):

import random

while your_health or enemy_health >= 10:
    print("He is still attacking you.")
    print("you may either:"
          "a-dodge"
          "b-attack back")
    fight_choice_1_1 = input("What do you do?")

    if fight_choice_1_1 == "a":
        d20_1_1 = random.randint(1, 20)
        if d20_1_1 >= 10:
            print("you dodge the attack")
        elif d20_1_1 <= 10:
            your_health -= knife_damage
            print("you get hit")
            if your_health <= 0:
                print("you lose. :(")
            elif enemy_health <= 0:
                print("you win!")

    if fight_choice_1_1 == "b":
        d20_1_1 = random.randint(1, 20)
        if d20_1_1 >= 10:
            print("you get …
Run Code Online (Sandbox Code Playgroud)

python loops while-loop

1
推荐指数
1
解决办法
150
查看次数

错误:尝试使用compareTo将一个字符串与另一个字符串进行比较时出现不兼容的类型

这是我的代码接受50个名字和滚动号.并按字母顺序打印.它为if(name [j] .compareTo(small))提供错误不兼容的类型

import java .io.*;
class student
{
    public void main()throws IOException
    {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      String name[]=new String[50];
      int mark[]=new int[50];
      int i;
      for( i=0;i<=49;i++)
      {
        System.out.println("plz ntr d name of d studnt");
        name[i]=br.readLine();
        System.out.println("plz ntr d marks of d studnt");
        mark[i]=Integer.parseInt(br.readLine());
        int j,pos=0;
        String temp, small;
        for(i=0;i<49;i++)
        {
          small=name[i];
          pos=i;
          for(j=i+1;j<49;j++)
          {
            if(name[j].compareTo(small))
              pos=j;
          }
        }
        temp=name[i];
        name[i]=name[pos];
        name[pos]=temp;
      }
      for(i=0;i<=49;i++)
      {
        System.out.println((i+1)+" "+name[i]+" "+mark[i]);
      }
    }
 }
Run Code Online (Sandbox Code Playgroud)

java bluej

1
推荐指数
1
解决办法
1994
查看次数

Haskell - 从句法糖翻译

任务是翻译这个

f = do
    c <- [1 .. 200]
    b <- [1 .. 200]
    guard (c >= b)
    a <- [1 .. 200]
    guard (a >= b && (c^2 - a^2 == b^2))
    return (a,b, c)
Run Code Online (Sandbox Code Playgroud)

成为一个非糖的版本.

我想我已经弄清楚了大部分问题,但是我在中间遇到了一个我需要修复的问题才能继续.到目前为止,我有:

f = [1 .. 200] >>= \c ->
    [1 .. 200] >>= \b ->
    if (c >= b) 
        then [1 .. 200] >>= \a -> if (a >= b && (c^2 - a^2 == b^2)) 
                                      then return(a,b,c)
                                      else return ()
        else return …
Run Code Online (Sandbox Code Playgroud)

haskell syntactic-sugar

1
推荐指数
2
解决办法
321
查看次数

这个循环在C中有什么问题?

int i;
i=0;
for (i=0;i>2;i++)
    {
     repeat((3),"|",var);
     printf("\n");          
    }
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它会进入"for"并跳过它.我试图将int ifor 的外部放在for之外,甚至在for之外初始化它,在调试中它是零.我需要它做的就是遍历这段代码两次.

c loops

1
推荐指数
1
解决办法
132
查看次数

将变量的结果附加到列表中

我试图将变量的结果添加f到列表的末尾list_name.但是,当我尝试这样做时,我收到以下错误:

list_name.append[f]  
Run Code Online (Sandbox Code Playgroud)

TypeError:'builtin_function_or_method'对象不可订阅

我究竟做错了什么?

python variables list append python-3.x

1
推荐指数
1
解决办法
9922
查看次数

C错误中的套接字

我是第一次做C套接字,我的代码遇到了一个小错误.当我编译它正常工作并编译好它只是抛出一些错误,我想知道如何解决它们.

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORTNUM 2343

int main(int argc, char *argv[])
{
    char msg[] = "Hello World !\n";

    struct sockaddr_in dest; /* socket info about the machine connecting to us */
    struct sockaddr_in serv; /* socket info about our server */
    int mysocket;            /* socket used to listen for incoming connections */
    int socksize = sizeof(struct sockaddr_in);

    memset(&serv, 0, sizeof(serv));    /* zero the struct before …
Run Code Online (Sandbox Code Playgroud)

c sockets networking

1
推荐指数
1
解决办法
4792
查看次数