小编Tra*_*v92的帖子

Perl同时替换多个字符串

有没有办法替换字符串中的多个字符串?例如,我有字符串hello world what a lovely day,我想替换whatlovely其他东西..

$sentence = "hello world what a lovely day";
@list = ("what", "lovely"); # strings to replace
@replist = ("its", "bad"); # strings to replace with
($val = $sentence) =~ "tr/@list/@replist/d";
print "$val\n"; # should print "hello world its a bad day"..
Run Code Online (Sandbox Code Playgroud)

任何想法为什么它不起作用?

谢谢.

regex arrays perl replace string-literals

12
推荐指数
2
解决办法
2万
查看次数

XAML数据绑定到全局变量?

如何将TextBoxes Text绑定到XAML中我的类中的全局变量?这是适用于Windows Phone的方式.

这是代码:

    namespace Class
    {
    public partial class Login : PhoneApplicationPage
    {
        public static bool is_verifying = false;

        public Login()
        {
            InitializeComponent();        
        }


        private void login_button_Click(object sender, RoutedEventArgs e)
        {
            //navigate to main page
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void show_help(object sender, EventArgs e)
        {
            is_verifying = true;
        }
      }

    }
Run Code Online (Sandbox Code Playgroud)

我想将Textboxes文本绑定到"is_verifying".

谢谢.

c# data-binding silverlight xaml windows-phone-8

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

Perl readdir按顺序排列

有没有办法保证退回的列表中的订单readdir

我有代码:

opendir(my $DIR, $src) or die "Error opening $src";

# Loop for each file in the directory
while (my $file = readdir($DIR))
{
        print "$file\n";
    }
Run Code Online (Sandbox Code Playgroud)

但它以随机顺序返回.现在我知道通过快速谷歌搜索有很多解决方案,但我找不到我需要的确切顺序.基本上我希望文件夹显示为FIRST或LAST,而不是文件之间.

例如,现在,如果我有文件夹结构:

folder
folder
file1
file2
file3
Run Code Online (Sandbox Code Playgroud)

我得到了结果:

file2
folder
folder
file1
file3
Run Code Online (Sandbox Code Playgroud)

我真的想要的时候:

folder
folder
file1
file2
file3
Run Code Online (Sandbox Code Playgroud)

要么:

file1
file2
file3
folder
folder
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这个目标?

sorting directory perl readdir

8
推荐指数
3
解决办法
6939
查看次数

在被另一个bash脚本调用后获取当前的文件目录

所以我有一个bash脚本调用另一个bash脚本.第二个脚本位于不同的文件夹中.

script1.sh:
"some_other_folder/script2.sh"
# do something

script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something
Run Code Online (Sandbox Code Playgroud)

在第二个脚本中它有行src=$(pwd),因为我从另一个目录中的另一个脚本调用该脚本,所以$(pwd)返回第一个脚本的当前目录.

有没有办法在该脚本中使用简单命令获取第二个脚本的当前目录而不必传递参数?

谢谢.

directory bash pwd

8
推荐指数
2
解决办法
5989
查看次数

C从链接列表中删除节点

如何从链表中删除节点?

这是我的代码:

void RemoveNode(Node * node, Node ** head) {
    if (strcmp(node->state, (*(*head)->next).state) == 0) {
        Node * temp = *head;
        *head = (*head)->next;
        free(temp);
        return;
    }

    Node * current = (*head)->next;
    Node * previous = *head;
    while (current != NULL && previous != NULL) {
        if (strcmp(node->state, (*current->next).state) == 0) {
            Node * temp = current;
            previous->next = current->next;
            free(temp);
            return;
        }
        current = current->next;
        previous = previous->next;
    }
    return;
}
Run Code Online (Sandbox Code Playgroud)

但我不断遇到seg故障.

我觉得我做的事情很蠢......任何想法?

c pointers linked-list nodes

7
推荐指数
1
解决办法
4万
查看次数

如何从链表中弹出?

我已经在C语言中用Pop函数实现了一个链表:

Node * pop (Node * head) {
    Node * temp = head;

    printf("Temp is: %s\n", temp->val);

    if (head->next != NULL) {
        *head = *head->next;
    }

    printf("Temp is: %s\n", temp->val);
    return temp;
}
Run Code Online (Sandbox Code Playgroud)

当我弹出时的输出将是这样的:

Temp is: node1 value
Temp is: node2 value
Run Code Online (Sandbox Code Playgroud)

也就是说,当我分配时,temp正在变为temp-> next *head = *head->next

那么,如何head在将Linked-list的头部移至的同时获取电流的值并返回呢head->next

这样head = head->next做不会删除对第一个节点的引用。(即,当我打印列表时,第一个节点仍然存在)。

c pointers return linked-list

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

C#使用Select或Where not working进行谓词化

在C#中使用谓词遇到一些问题.我有两组代码(我认为两者都应该完成相同的结果),但一个代码永远不会有效.我问的原因是我需要这个谓词出现几次不同的元素,所以我想尽可能保持最小(非工作的非常简单,而另一个包含很多行).

1不工作:

ItemViewModel item = (App.ViewModel.All_Items.Where(x => (x as ItemViewModel).Name == my_list_of_strings.ElementAt(2)) as ItemViewModel);
Run Code Online (Sandbox Code Playgroud)

同时使用"选择"而不是"何处"不起作用.

2工作:

foreach (ItemViewModel it in App.ViewModel.All_Items)
{
  if (item.Name == my_list_of_strings.ElementAt(2))
  {
    MessageBox.Show("Success!!");
    item = it;
    continue; // Leave loop
  }
}
Run Code Online (Sandbox Code Playgroud)

我可能忽略了一些愚蠢的东西,但如果有人知道解决方案,那就太棒了!

谢谢.

c# for-loop predicate where-clause

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

Matlab输出-空间填充?

我正在尝试输出一个矩阵:

M = [1 20 3; 22 3 24; 100 150 2];
Run Code Online (Sandbox Code Playgroud)

使用方法:

for i=1:3
    fprintf('%f\t%f\t%f\n', M(i), M(i+length(M)), M(i+length(M)*2));
end
Run Code Online (Sandbox Code Playgroud)

输出结果如下:

1 20 3
22  3  24
100  150  2
Run Code Online (Sandbox Code Playgroud)

这显然不是很好。我如何获得它,以便在整数的前面填充空格?像这样:

  1   20   3
 22    3  24
100  150   2
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢!

format matlab printf matrix output

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